Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDatePicker Change color of text of "today" date

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.

like image 974
Ricardo Alves Avatar asked Nov 28 '25 00:11

Ricardo Alves


2 Answers

(It has been reported that this approach crashes in iOS 14 so it is no longer valid. Please see the discussion below.)

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")
like image 200
inket Avatar answered Nov 30 '25 14:11

inket


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.

like image 29
kalsky Avatar answered Nov 30 '25 14:11

kalsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!