【问题标题】:How do I change the font color of UIPickerView and UIDatePicker?如何更改 UIPickerView 和 UIDatePicker 的字体颜色?
【发布时间】:2017-05-17 06:51:31
【问题描述】:

我有一个viewController 和一个UIPickerView 和一个UIDatePicker,如下图我的storyboard 的屏幕截图所示。 我尝试按照here 的说明添加运行时属性。但跟随它只改变了最初突出显示的文本的字体颜色。 我想将我的 pickerView 和 datePicker 中的文本颜色更改为白色。我不知道如何实现它。

【问题讨论】:

    标签: ios swift3 uipickerview uidatepicker


    【解决方案1】:

    您需要编写此行来更改日期选择器文本颜色,

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

    它会改变UIDatePicker的文字颜色

    对于PickeView,您必须设置属性字符串,例如,

    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    
        return NSAttributedString(string: "Your Text", attributes: [NSForegroundColorAttributeName:UIColor.blue])
    }
    

    【讨论】:

    • 非常感谢。可悲的是,Apple 不会为此提供简单的setTextColor 方法。我不敢相信他们认为只有黑色字体是可以接受的......
    【解决方案2】:

    这两行字为我节省了一天。

        NepaliDatePicker.setValue(UIColor.white, forKey: "textColor")
        EnglishDatePicker.setValue(UIColor.white, forKey: "textColor")
    

    【讨论】:

      【解决方案3】:

      您可以为“UIPickerView”使用“viewForRow”委托方法

      func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
           // Your custom view with custom font here
      }
      

      对于 UIDatePicker,您无法更改字体,但您可以使用此代码更改 textColor。

      datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
      datePicker.datePickerMode = .CountDownTimer
      datePicker.datePickerMode = .DateAndTime
      

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        您可以使用选取器视图的属性TitleForRow 委托方法来更改颜色。请参阅此链接How do I change the color of the text in a UIPickerView under iOS 7?。您还可以使用 viewForRow 委托方法并添加自定义 UILabel 作为视图。

        【讨论】:

        • 这是我在问题中作为参考发布的同一个问题。
        • 好的。你得到你的答案了吗? @ama再次
        猜你喜欢
        • 2014-01-19
        • 2021-11-19
        • 1970-01-01
        • 1970-01-01
        • 2016-02-20
        • 1970-01-01
        • 1970-01-01
        • 2020-12-09
        • 1970-01-01
        相关资源
        最近更新 更多