38

Is it any way to change text color of datepicker in iOS8? I've that it isn't possible in iOS7 and prior, something changed in 8th version?

For example I've found modified picker in Yahoo Weather!

enter image description here

Vasily
  • 3,740
  • 3
  • 27
  • 61

8 Answers8

78

Still Works, Swift 4 Update

datePicker.setValue(UIColor.white, forKeyPath: "textColor")

This works for me.

datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
Matthew Bradshaw
  • 1,843
  • 3
  • 13
  • 21
Sylvio LeBlanc
  • 789
  • 1
  • 4
  • 2
33

It's working on iOS 10 only if you set both properties.

datePicker.setValue(UIColor.white, forKeyPath: "textColor")
datePicker.setValue(false, forKeyPath: "highlightsToday")
belebeich
  • 391
  • 3
  • 8
  • The second line gives me an error: Thread 1: "-[_UIDatePickerCalendarView setHighlightsToday:]: unrecognized selector sent to instance 0x7b50000c3800" – אורי orihpt Dec 04 '20 at 12:16
24

I've changed text color through inspector (see below)

enter image description here

Włodzimierz Woźniak
  • 3,106
  • 1
  • 26
  • 23
22

If you don't want the selected text to be black at start you need to set both textColor and highlightsToday :

enter image description here

CedricSoubrie
  • 6,657
  • 2
  • 39
  • 44
11

Found solution at comments of stackoverflow. If you need just to change text color to yours and assign this subclass you your picker. For whiteColor works as magic.

Only minus i've found that color two lines of selected number is still gray.

class ColoredDatePicker: UIDatePicker {
    var changed = false
    override func addSubview(view: UIView) {
        if !changed {
            changed = true
            self.setValue(UIColor.whiteColor(), forKey: "textColor")
        }
        super.addSubview(view)
    }
}
Community
  • 1
  • 1
Vasily
  • 3,740
  • 3
  • 27
  • 61
  • 2
    The "only minus"? Anything that doesn't look perfect is a *huge* minus for discerning apps and customers. It isn't adequately supported by Apple *yet*. – clearlight Oct 31 '15 at 03:41
3

This worked for me:

setting text color for all date picker subviews

for view in datePicker.subviews {
    view.setValue(UIColor.white, forKeyPath: "textColor")
}

Possible duplicate: Set text color and font for UIDatePicker in iOS8/Swift

Wojtek Dmyszewicz
  • 4,188
  • 5
  • 30
  • 42
1

Resolving the issue with black "Today" via highlightsToday set to "false" has a side-effect - it's no longer has "Today" label in the list.

I've found a different solution here which works perfect without any side-effects: Set text color and font for UIDatePicker in iOS8/Swift

datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime //or whatever your original mode was

Looks like a dirty hack but that's we have at the moment: changing datePickerMode re-draws it correctly. So just set it to something different from your desired mode and change it back to original.

Alex
  • 475
  • 3
  • 14
0

I found a way that wasnt listed here or in the other thread. https://stackoverflow.com/a/59838215/4266735

I ran into a similar issue with the latest SwiftUI / Swift 5 on XCode 11. All of the options above did not work and the DatePicker either stayed black text or crashed.

In your SwiftUI file set init() before var body

init() {
    UIDatePicker.appearance().backgroundColor = .clear
}

Then in your var body view do this

DatePicker(selection: $dob, in: ...Date(), displayedComponents: .date) {
   Text("Select Date")
}.colorInvert()

That inverted the black text to be white using the iOS Dark Theme. Looks/works great. Hope this helps.