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
}
iOS14 - How to change textColor of UIDatePicker in UIKit?
Try this, add the extension below:
extension UIDatePicker {
var textColor: UIColor? {
set {
setValue(newValue, forKeyPath: "textColor")
}
get {
return value(forKeyPath: "textColor") as? UIColor
}
}
}
Now in viewDidLoad call:
myDatePicker.textColor = .yourColor
call it after other picker property, es.:
myDatePicker.preferredDatePickerStyle = .wheels
myDatePicker.addTarget(self, action: #selector(handleDatePicker), for: .valueChanged)
myDatePicker.datePickerMode = .dateAndTime
myDatePicker.timeZone = NSTimeZone.local
myDatePicker.backgroundColor = .black
myDatePicker.setValue(false, forKey: "highlightsToday")
myDatePicker.textColor = .yourColor // here
if you want highlightsToday set the relative picker value to true:
myDatePicker.setValue(true, forKey: "highlightsToday")
and this is the result:
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 selected Date text Color for Dark mode (iOS 13)
I found something that solves the problem in a way. If I mark 'hightlightsToday' color to false, then the selected text shows with the color you set your code.
self.datePicker.setValue(false, forKey: "highlightsToday")
But if I wanted to highlight my selected date text color with a different color; in that case not sure which key value I have to change. So I am going to leave the question open, incase anyone knows how to change the selected date text color for dark mode.
Changing font color of Date Picker with inline style
There is no way to change inline
styled Date Picker text color in UIKit
14, but here is a workaround you can try:
Override style
I noticed that if you change the theme of the device (between light and dark), the text color of the .inline
date picker is changing.
So for example if you want a dark-text date picker in dark theme, you can override it:
myDatePicker.overrideUserInterfaceStyle = .light
Heres a image of the result
I will keep this post updated if there is any new way to change to any color.
How do I change the font color of UIPickerView and UIDatePicker?
you need to write this line to change date picker text color,
self.datePicker.setValue(UIColor.white, forKeyPath: "textColor")
It will change textcolor of UIDatePicker
And For PickeView
, You have to set attributed string like,
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return NSAttributedString(string: "Your Text", attributes: [NSForegroundColorAttributeName:UIColor.blue])
}
Related Topics
Swift - Uiimagepickercontroller - How to Use It
Loading a Resource (E.G. Storyboard) in a Swift Framework
Uitextfield Should Accept Number Only Values
Xcode: Could Not Inspect the Application Package
How to Create and Send the JSON Data to Server Using Swift Language
Xcode 9 - Localization Issue Warning Storyboard
How to Submit iOS App for Review - "There Are One or More Errors on the Page."
Removing from Array During Enumeration in Swift
Are Afnetworking Success/Failure Blocks Invoked on the Main Thread
Unified Uiviewcontroller "Became Frontmost" Detection
SkstoreReviewController, How to Use It in a Correct Way
Application Sticks on Osspinlocklockslow
Using Core Data with Watchos 2.0
"Initialize" Class Method for Classes in Swift
Making Video from Uiimage Array with Different Transition Animations