Set Text Color and Font for Uidatepicker in iOS8/Swift

Set text color for UIDatePicker in Swift

I have face the same issue so i have try something like this, Changing the datePickerMode to something else, it will re-draw with the newly set textColor, also instead of setting textColor in updatedatePicker function set this also in viewDidLoad like this.

Answer updated to latest Swift 4

override func viewDidLoad() {
super.viewDidLoad()
datePicker.setValue(UIColor.white, forKeyPath: "textColor")
datePicker.datePickerMode = .date
datePicker.datePickerMode = .dateAndTime //Set here that mode you want.
datePicker.addTarget(self, action: #selector(selectedDate), for: .valueChanged)
}

Change UIDatePicker font color?

All I need (on iOS 8.x and 9.0) is this one line to change the font color:

        [my_date_picker setValue:[UIColor whiteColor] forKey:@"textColor"];

No subclassing or invoking of private APIs...


Note: today's current date will still be highlighted (in newer iOS versions). You can get around this by using the following code:

if ([my_date_picker respondsToSelector:sel_registerName("setHighlightsToday:")]) {

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"

[my_date_picker performSelector:@selector(setHighlightsToday:) withObject:[NSNumber numberWithBool:NO]];

#pragma clang diagnostic pop

}

Changing text color of datepicker

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)
}
}

Set the font for UIDatePicker in iOS10 with Swift 3

So, is there a formal way of changing the font of a UIDatePicker?

No, there isn't. That is exactly why all these workarounds are proposed.



Related Topics



Leave a reply



Submit