Fen*_*nda 25 uidatepicker ios swift
我一直在设置UIDatePicker 字体和颜色时遇到麻烦.我的应用程序中的其他所有内容都非常简单,除此之外.有人知道怎么做这个吗?我正在使用Swift for iOS8.

Dyl*_*ich 28
将日期模式更改为其他内容似乎强制使用新设置的文本颜色重新绘制.
datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime //or whatever your original mode was
Pra*_*ble 14
您只需要在viewdidLoad/ viewWillAppearaccoding中使用DatePicker 设置2行代码.
dobDatePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
dobDatePicker.setValue(false, forKey: "highlightsToday")
看到这样的结果:
更改 UIDatePickerView 字体的唯一方法(直到现在)是 swizzling:
你可以通过 UILabel 的扩展来改变字体!(这不是推荐的,但它有效!)
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
    }
}
要更改颜色,您只需调用以下函数:
datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
如果需要重新渲染,您需要调用:
datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime //or whatever your original mode was
小智 5
尝试这个:
    /* 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")
小智 5
您可以使用
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()
我相信这是倒数计时器的最终解决方案。
这是yildirimosman的答案的扩展。
//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()
| 归档时间: | 
 | 
| 查看次数: | 27780 次 | 
| 最近记录: |