在iOS8/Swift中为UIDatePicker设置文本颜色和字体

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
Run Code Online (Sandbox Code Playgroud)

  • 字体怎么样? (15认同)
  • 我无法相信这是我们需要用'setValue forKeypath`来破解的东西.它不像我们试图用表情符号或随机的东西替换日期值. (6认同)

Pra*_*ble 14

您只需要在viewdidLoad/ viewWillAppearaccoding中使用DatePicker 设置2行代码.

dobDatePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
dobDatePicker.setValue(false, forKey: "highlightsToday")
Run Code Online (Sandbox Code Playgroud)

看到这样的结果:

在此输入图像描述

  • 这个工作!即使在Xcode 8.x上的Swift 3上也是如此 (2认同)

Ali*_*nco 8

更改 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
    }
}
Run Code Online (Sandbox Code Playgroud)

要更改颜色,您只需调用以下函数:

datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
Run Code Online (Sandbox Code Playgroud)

如果需要重新渲染,您需要调用:

datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime //or whatever your original mode was
Run Code Online (Sandbox Code Playgroud)


小智 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")
Run Code Online (Sandbox Code Playgroud)


小智 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()
Run Code Online (Sandbox Code Playgroud)


Obj*_*eTC 5

我相信这是倒数计时器的最终解决方案。
这是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()
Run Code Online (Sandbox Code Playgroud)


Dav*_*son 3

API 没有提供执行此操作的方法。您可以使用 UIPickerView 而不是使用 UIDatePicker 自己制作一个非常令人信服的副本。在这里

  • 对于其他任何事情,复制品都可以,但是对于日期?!当然不是 - 正如我们所知,当独立开发者试图自己搞乱日期时,有很多事情可能会出错 - 时区更改、夏令时等等...... UIDatePicker 负责处理所有这些并提供一个可靠的 NSDate 对象。遗憾的是它没有一些基本的自定义选项。 (2认同)