118

I'm aware of the pickerView:viewForRow:forComponent:reusingView method, but when using the view it passes in reusingView: how do I change it to use a different text color? If I use view.backgroundColor = [UIColor whiteColor]; none of the views show up anymore.

Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • possible duplicate of [can I change the font color of the datePicker in iOS7?](http://stackoverflow.com/questions/18807940/can-i-change-the-font-color-of-the-datepicker-in-ios7) – Aaron Brager Oct 08 '13 at 00:49
  • 2
    @AaronBrager. Que Not a duplicate the link provided by u is asked after this que. – Gajendra K Chauhan Sep 08 '15 at 04:47

8 Answers8

329

There is a function in the delegate method that is more elegant:

Objective-C:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title = @"sample title";
    NSAttributedString *attString = 
        [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    return attString;
}

If you want to change the selection bar colors as well, I found that I had to add 2 separate UIViews to the view containing the UIPickerView, spaced 35 pts apart for a picker height of 180.

Swift 3:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let string = "myString"
    return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName:UIColor.white])
}

Swift 4:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let string = "myString"
    return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
}

Swift 4.2:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    let string = "myString"
    return NSAttributedString(string: string, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
}

Remember when you use the method: You don't need to implement titleForRowInComponent() as it is never called when using attributedTitleForRow().

KlimczakM
  • 12,576
  • 11
  • 64
  • 83
Jon
  • 7,848
  • 1
  • 40
  • 41
29

Original post here: can I change the font color of the datePicker in iOS7?

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
    label.backgroundColor = [UIColor grayColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
    label.text = [NSString stringWithFormat:@" %d", row+1];
    return label;
}

// number Of Components
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// number Of Rows In Component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:   (NSInteger)component
{
    return 6;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Bordz
  • 2,810
  • 4
  • 23
  • 27
  • This solution works fine for a pickerview containing a single component. If you have more than 1, the labels will overlap from what I can see. – PKCLsoft Feb 04 '14 at 04:23
23
  1. Go to storyboard
  2. Select PickerView
  3. Go to Identity inspector (3rd tab)
  4. Add User Defined Runtime Attribute
  5. KeyPath = textColor
  6. Type = Color
  7. Value = [Color of you choice]

screenshot

Pang
  • 9,564
  • 146
  • 81
  • 122
Lancewise
  • 231
  • 2
  • 2
7

in Xamarin, override the UIPickerModelView method GetAttributedTitle

public override NSAttributedString GetAttributedTitle(UIPickerView picker, nint row, nint component)
{
    // change text white
    string title = GetTitle (picker, row, component); // get current text from UIPickerViewModel::GetTitle
    NSAttributedString ns = new NSAttributedString (title, null, UIColor.White); // null = font, use current font
    return ns;
}
Matt
  • 169
  • 2
  • 6
7

It works for me

pickerView.setValue(UIColor.yellow, forKeyPath: "textColor")
logeshpalani31
  • 1,416
  • 12
  • 33
2

I ran into the same problem with a pickerView using two components. My solution is similar to above with a few modifications. Because I am using two components it is necessary to pull from two different arrays.

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor blueColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];

    //WithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 60)];

    if(component == 0)
    {
        label.text = [countryArray objectAtIndex:row];
    }
    else
    {
        label.text = [cityArray objectAtIndex:row];
    }
    return label;
}
user13500
  • 3,817
  • 2
  • 26
  • 33
R Thomas
  • 445
  • 4
  • 4
2

Swift 4 (Update to the accepted answer)

extension MyViewController: UIPickerViewDelegate{
    }

    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        return NSAttributedString(string: "your-title-goes-here", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
harsh_v
  • 3,193
  • 3
  • 34
  • 53
1
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
        UILabel* pickerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 37)];
        pickerLabel.text = @"text";
        pickerLabel.textColor = [UIColor redColor];
        return pickerLabel;
}
Bobby
  • 6,115
  • 4
  • 35
  • 36