如何更改 UIPickerView 和 UIDatePicker 的字体颜色?

How do I change the font color of UIPickerView and UIDatePicker?

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

您可以使用 "viewForRow" 委托方法 "UIPickerView"

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

对于 UIDatePicker,您不能更改字体,但可以使用此代码更改文本颜色。

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

希望对您有所帮助。

您可以使用选择器视图的 attributedTitleForRow 委托方法来更改颜色。参考这个 link How do I change the color of the text in a UIPickerView under iOS 7?。您还可以使用 viewForRow 委托方法并添加自定义 UILabel 作为视图。

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

 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])
}

这两行拯救了我的一天。

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