Set text color and font for UIDatePicker in iOS8/Swift

后端 未结 16 2184
囚心锁ツ
囚心锁ツ 2020-12-03 21:02

I\'ve been having trouble trying to set the UIDatePicker font and color. Everything else in my app was fairly straightforward to adjust except

相关标签:
16条回答
  • 2020-12-03 21:05

    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")
    
    0 讨论(0)
  • 2020-12-03 21:08

    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
    
    0 讨论(0)
  • 2020-12-03 21:08

    Swift 4

    override func layoutSubviews() {
        super.layoutSubviews()
    
        datePickerView.setValue(UIColor.white, forKeyPath: "textColor")
    }
    
    0 讨论(0)
  • 2020-12-03 21:13

    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.

    0 讨论(0)
  • 2020-12-03 21:14

    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
    
    0 讨论(0)
  • 2020-12-03 21:16

    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.

    0 讨论(0)
提交回复
热议问题