UIDatePicker Change color of text of “today” date

2019-09-16 09:10发布

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.

4条回答
Lonely孤独者°
2楼-- · 2019-09-16 09:33

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")
查看更多
Deceive 欺骗
3楼-- · 2019-09-16 09:37

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楼-- · 2019-09-16 09:39

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.

查看更多
Explosion°爆炸
5楼-- · 2019-09-16 09:43

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

查看更多
登录 后发表回答