代码之家  ›  专栏  ›  技术社区  ›  Vasily

更改日期选择器的文本颜色

  •  38
  • Vasily  · 技术社区  · 11 年前

    有没有办法在iOS8中更改日期选择器的文本颜色?我知道这在iOS7和之前的版本中是不可能的,在第8版中发生了什么变化?

    例如,我在Yahoo Weather!

    enter image description here

    8 回复  |  直到 11 年前
        1
  •  77
  •   Matthew Bradshaw Sylvio LeBlanc    8 年前

    Still Works,Swift 4更新

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

    这对我有用。

    datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
    
        2
  •  31
  •   belebeich    8 年前

    只有设置了这两个属性,它才能在iOS 10上运行。

    datePicker.setValue(UIColor.white, forKeyPath: "textColor")
    datePicker.setValue(false, forKeyPath: "highlightsToday")
    
        3
  •  24
  •   Włodzimierz Woźniak    10 年前

    我已经通过检查器更改了文本颜色(见下文)

    enter image description here

        4
  •  21
  •   CedricSoubrie    9 年前

    如果您不希望所选文本在开始时为黑色,则需要设置textColor和highlights Today:

    enter image description here

        5
  •  11
  •   Community Mohan Dere    9 年前

    在找到解决方案 comments stackoverflow的。如果你需要 将文本颜色更改为您的颜色,并将此子类分配给您的选择器。对于白色来说,颜色就像魔法一样。

    只有减,我发现所选数字的两行仍然是灰色的。

    class ColoredDatePicker: UIDatePicker {
        var changed = false
        override func addSubview(view: UIView) {
            if !changed {
                changed = true
                self.setValue(UIColor.whiteColor(), forKey: "textColor")
            }
            super.addSubview(view)
        }
    }
    
        6
  •  3
  •   Wojtek Dmyszewicz    6 年前

    这对我有用:

    设置所有日期选择器子视图的文本颜色

    for view in datePicker.subviews {
        view.setValue(UIColor.white, forKeyPath: "textColor")
    }
    

    可能重复: Set text color and font for UIDatePicker in iOS8/Swift

        7
  •  1
  •   Alex    6 年前

    通过highlightsToday设置为“false”来解决黑色“Today”的问题有一个副作用——列表中不再有“Today(今天)”标签。

    我在这里找到了另一种完美无副作用的解决方案: Set text color and font for UIDatePicker in iOS8/Swift

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

    看起来像是一个肮脏的黑客,但目前我们已经做到了这一点:更改datePickerMode会重新正确绘制它。所以只需将其设置为不同于您所需的模式,然后将其更改回原始模式。

        8
  •  0
  •   Cody P. Christian    6 年前

    我找到了一种没有在这里或其他线程中列出的方法。 https://stackoverflow.com/a/59838215/4266735

    我在XCode 11上使用最新的SwiftUI/Swift 5时遇到了类似的问题。上述所有选项都不起作用,DatePicker要么保持黑色文本,要么崩溃。

    在SwiftUI文件中,在var body之前设置init()

    init() {
        UIDatePicker.appearance().backgroundColor = .clear
    }
    

    然后在var body视图中执行以下操作

    DatePicker(selection: $dob, in: ...Date(), displayedComponents: .date) {
       Text("Select Date")
    }.colorInvert()
    

    使用iOS黑暗主题将黑色文本反转为白色。看起来/工作很好。希望这有帮助。

    推荐文章