All I want to do is change the font color of the UIDatePicker. I\'ve researched other questions but they\'re all involving changing other properties and customizing the enti
Next solution comes from "arturgrigor" and it works great in my apps, just copy it, paste it in viewDidLoad method, and enjoy it :
[my_datePicker setValue:[UIColor whiteColor] forKeyPath:@"textColor"];
SEL selector = NSSelectorFromString( @"setHighlightsToday:" );
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature :
[UIDatePicker
instanceMethodSignatureForSelector:selector]];
BOOL no = NO;
[invocation setSelector:selector];
[invocation setArgument:&no atIndex:2];
[invocation invokeWithTarget:my_datePicker];
I stumbled upon a surprisingly clean solution using UIAppearance
, without using any KVC, swizzling, or otherwise private API. I found that attempting to set the textColor
via UIAppearance
for any UILabel
within a UIDatePicker
had no affect, but a custom appearance property that simply called the regular textColor
setter worked just fine.
// Implement a custom appearance property via a UILabel category
@interface UILabel (PickerLabelTextColor)
@property (nonatomic, strong) UIColor * textColorWorkaround UI_APPEARANCE_SELECTOR;
@end
@implementation UILabel (PickerLabelTextColor)
- (UIColor *)textColorWorkaround {
return self.textColor;
}
- (void)setTextColorWorkaround:(UIColor *)textColor {
self.textColor = textColor;
}
@end
And then use as follows:
UILabel *pickerLabelProxy = [UILabel appearanceWhenContainedInInstancesOfClasses:@[UIDatePicker.class]];
pickerLabelProxy.textColorWorkaround = UIColor.lightGrayColor;
UILabel extension:
extension UILabel {
@objc dynamic var textColorWorkaround: UIColor? {
get {
return textColor
}
set {
textColor = newValue
}
}
}
Appearance proxy use:
let pickerLabelProxy = UILabel.appearance(whenContainedInInstancesOf: [UIDatePicker.self])
pickerLabelProxy.textColorWorkaround = UIColor.lightGray
[date_picker setValue:textColor forKey:@"textColor"];
[date_picker performSelector:@selector(setHighlightsToday:) withObject:NO];