If you only want to change the text color just use the other delegate method pickerView:attributedTitleForRow:forComponent:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
UIFont *font;
if (row == 1) {
title = [NSString stringWithFormat:@" %@",[self.servicesArray objectAtIndex:row]];
font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
} else{
title = [NSString stringWithFormat:@" -%@",[self.servicesArray objectAtIndex:row] ];
font = [UIFont fontWithName:kAppFont size:16];
}
UIColor *textColor;
if (self.isBackgroundWhite) {
textColor = [UIColor blackColor];
} else {
NSLog(@"Set white text");
textColor = [UIColor whiteColor];
}
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attributes = @{NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle};
return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
Also a note that you should call [self.pickerView reloadAllComponents];
when changing isBackgroundWhite
. I would do this by overriding the setter for backgroundWhite
and when the boolean value changes reload the picker view.
EDIT:
There appears to be a 'bug' (intentional or not) in iOS 7 (works fine on iOS 6) with setting the UIFont
on an NSAttributedString
returned from the pickerView:attributedTitleForRow:forComponent:
method. So while the above works for the text color the font does not change. Luckily you can get around this with the code in the question. See this answer for more info.