How do I change the color of the text in a UIPickerView under iOS 7?

IosObjective CUiviewIos7Uipickerview

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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().

Solution 2 - Ios

Original post here: https://stackoverflow.com/questions/18807940/can-i-change-the-font-color-of-the-datepicker-in-ios7/19147577#19147577

- (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;
}

Solution 3 - Ios

  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

Solution 4 - Ios

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;
}

Solution 5 - Ios

It works for me

pickerView.setValue(UIColor.yellow, forKeyPath: "textColor")

Solution 6 - Ios

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;
}

Solution 7 - Ios

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])
    }
}

Solution 8 - Ios

- (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;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDoug SmithView Question on Stackoverflow
Solution 1 - IosJonView Answer on Stackoverflow
Solution 2 - IosBordzView Answer on Stackoverflow
Solution 3 - IosLancewiseView Answer on Stackoverflow
Solution 4 - IosMattView Answer on Stackoverflow
Solution 5 - Ioslogeshpalani31View Answer on Stackoverflow
Solution 6 - IosR ThomasView Answer on Stackoverflow
Solution 7 - Iosharsh_vView Answer on Stackoverflow
Solution 8 - IosBobbyView Answer on Stackoverflow