How do I change the color of the text in a UIPickerView under iOS 7?
Solution 1:
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:
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;
}
Solution 3:
- Go to storyboard
- Select PickerView
- Go to Identity inspector (3rd tab)
- Add User Defined Runtime Attribute
- KeyPath = textColor
- Type = Color
- Value = [Color of you choice]
Solution 4:
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;
}