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
Highlight Just the Text in a Uilabel
Convert an Uiimage in a Texture
Launch Other Application Without Url Schema in Iphone
Change the Root View of Uihostingcontroller in Swiftui
How to Remove Border from Segmented Control
What Is Difference Between Urlwithstring and Fileurlwithpath of Nsurl
How to Properly Group a List Fetched from Coredata by Date
Nsoperationqueue Serial Fifo Queue
Corebluetooth: What Is the Lifetime of Unique Uuids
Ckquery from Private Zone Returns Only First 100 Ckrecords from in Cloudkit
Apple Llvm 6.0 Error: Clang Failed with Exit Code -1
iPhone Storage in Tmp Directory
Simple Low-Latency Audio Playback in iOS Swift
Invalid Image Path - No Image Found at the Path. Cfbundleicons Xcode 5