Set text color and font for UIDatePicker in iOS8/Swift

江枫思渺然 提交于 2019-12-28 04:22:06

问题


I've been having trouble trying to set the UIDatePicker font and color. Everything else in my app was fairly straightforward to adjust except this. Does anybody know how to do this? I'm using Swift for iOS8.


回答1:


Changing the date mode to something else seems to force a re-draw with the newly set text color.

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



回答2:


you just need to set 2 lines of code in viewdidLoad / viewWillAppear accoding where you using DatePicker.

dobDatePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
dobDatePicker.setValue(false, forKey: "highlightsToday")

See the Result like this:




回答3:


try this:

    /* set color for UIDatePicker font */

    //text color of today string
    self.datePicker.performSelector("setHighlightsToday:", withObject:Constants.Colors.mainHeaderColor)

    //text color for hoglighted color
    self.datePicker.performSelector("_setHighlightColor:", withObject:Constants.Colors.mainHeaderColor)

    //other text color
    self.datePicker.setValue(Constants.Colors.mainHeaderColor, forKey: "textColor")



回答4:


you can use

datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")
datePicker.setValue(false, forKey: "highlightsToday")
//for selector color
datePickerView.subviews[0].subviews[1].backgroundColor = UIColor.whiteColor()
datePickerView.subviews[0].subviews[2].backgroundColor = UIColor.whiteColor()



回答5:


I believe this is the definitive solution for countdown timers.
It's an expansion of yildirimosman's answer.

//text color
datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")
//picker background
datePicker.subviews[0].subviews[0].backgroundColor = UIColor.clearColor() //the picker's own background view
//dividers
datePicker.subviews[0].subviews[1].backgroundColor = UIColor.whiteColor()
datePicker.subviews[0].subviews[2].backgroundColor = UIColor.whiteColor()
//labels: "hours" and "min"
datePicker.subviews[0].subviews[3].setValue(UIColor.lightGrayColor(), forKey: "textColor")
datePicker.subviews[0].subviews[4].setValue(UIColor.lightGrayColor(), forKey: "textColor")
//refresh the tableview (to force initial row textColor to change to white)
datePicker.subviews[0].setNeedsLayout()
datePicker.subviews[0].layoutIfNeeded()



回答6:


Adding textColor and highlightsToday to user defined variables did the trick for me :




回答7:


The only way for changing the font of UIDatePickerView (until now) is swizzling:

you can change the font by an extension of UILabel! (this is not recommended but it works!)

import Foundation
import UIKit

public extension UILabel {

    @objc func setFontSwizzled(font: UIFont) {
        if self.shouldOverride() {
            self.setFontSwizzled(font: UIFont.fontWith(style: .regular, size: 14))
        } else {
            self.setFontSwizzled(font: font)
        }
    }

    private func shouldOverride() -> Bool {
        let classes = ["UIDatePicker", "UIDatePickerWeekMonthDayView", "UIDatePickerContentView"]
        var view = self.superview
        while view != nil {
            let className = NSStringFromClass(type(of: view!))
            if classes.contains(className) {
                return true
            }
            view = view!.superview
        }
        return false
    }

    private static let swizzledSetFontImplementation: Void = {
        let instance: UILabel = UILabel()
        let aClass: AnyClass! = object_getClass(instance)
        let originalMethod = class_getInstanceMethod(aClass, #selector(setter: font))
        let swizzledMethod = class_getInstanceMethod(aClass, #selector(setFontSwizzled))

        if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod {
            // switch implementation..
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }()

    static func swizzleSetFont() {
        _ = self.swizzledSetFontImplementation
    }
}

and for changing the color you just simply call the function below:

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

if it's necessary to be re-rendered you need to call:

datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime //or whatever your original mode was



回答8:


The API does not provide a way to do this. You can make a pretty convincing replica yourself using a UIPickerView rather than using UIDatePicker. Se here




回答9:


You can set value using forKeyPath: "textColor". The code:

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

where datePicker is your UIDatePicker object, and the first parameter is the color that you want




回答10:


I wanted to do this but set it for when someone has the date set prior to now, or after now. I had to reload the data, but when I did it ended up setting it to the current DateTime when using the above example.

So what I did was set a temporary value and then set it after the reload. It does make it do an animated effect, but it works. If you know a better way, let me know...

func dateChanged(sender: UIDatePicker) {
    print(sender.date.description)

    let tempDate = sender.date
    let currentDate = NSDate()

    if originalDate.isLessThanDate(currentDate) {
        originalDate = sender.date
        if sender.date.isGreaterThanDate(currentDate) {
            sender.setValue(UIColor.blackColor(), forKeyPath: "textColor")
            sender.datePickerMode = .CountDownTimer
            sender.datePickerMode = .DateAndTime
            sender.date = tempDate
            sender.reloadInputViews()
        }
    }

    if sender.date.isLessThanDate(currentDate) {
        sender.setValue(UIColor.redColor(), forKeyPath: "textColor")
        sender.datePickerMode = .CountDownTimer
        sender.datePickerMode = .DateAndTime
        sender.date = tempDate
        sender.reloadInputViews()
    }
}



回答11:


Swift 4

override func layoutSubviews() {
    super.layoutSubviews()

    datePickerView.setValue(UIColor.white, forKeyPath: "textColor")
}



回答12:


I saw the issue you were having and was having a similar issue. Using Xcode 6.3.1 I used this code in mine and worked great:

myPicker.backgroundColor = UIColor.whiteColor()

In case this helps.




回答13:


You can use extensions to get and set textColor like bellow

extension UIDatePicker {

    var textColor: UIColor? {
        set {
            setValue(newValue, forKeyPath: "textColor")
        }
        get {
            return value(forKeyPath: "textColor") as? UIColor
        }
    }

}

And then set the color:

datePicker.textColor = .red


来源:https://stackoverflow.com/questions/28417217/set-text-color-and-font-for-uidatepicker-in-ios8-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!