问题
I know that this question as been made other times, but with xCode 7 and iOS9.2 is it possible to change the font color of "today" date on a UIDatePicker?
[self.dpData setValue:[UIColor colorWithRed:111/255.0f green:113/255.0f blue:121/255.0f alpha:1.0f] forKeyPath:@"setHighlightsToday"];
I tried this and it crashed, so I know that this don't work.
Any other options that I have, or I really need to build my own DatePicker?
Thanks in advance.
回答1:
A safer/easier way to do it is to simply set the value of the key "highlightsToday". Here's a Swift 2.2 example:
datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")
datePicker.setValue(false, forKey: "highlightsToday")
回答2:
I used the same method Richardo until now, but it is using a private api that could always become unavailable or Apple might reject it. I just changed my code (as hacky and ugly as it is) to just change the date picker to a different mode and back - this causes the Today color to get the textColor that was set before.
selectedColor = [UIColor whiteColor];
[self.TimerPicker setValue:selectedColor forKeyPath:@"textColor"];
[self.TimerPicker setDatePickerMode:UIDatePickerModeDate];
[self.TimerPicker setDatePickerMode:UIDatePickerModeDateAndTime];
good enough for me.
回答3:
With some more search I found the solution.
[_dpData setValue:[UIColor colorWithRed:111/255.0f green:113/255.0f blue:121/255.0f alpha:1.0f] 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:_dpData];
With this solution we can change the color of the text (even today date) without problems.
回答4:
Going through the documentation for this class and it's parent class it says it's still not possible
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDatePicker_Class/index.html#//apple_ref/doc/uid/TP40006806
Yes you should make your own, since with an item picker it's possible.
This is really stupid on Apple's behalf
来源:https://stackoverflow.com/questions/33387791/uidatepicker-change-color-of-text-of-today-date