2017-05-17 73 views
1

我有一个viewControllerUIPickerViewUIDatePicker如下面从我的storyboard截图所示。 The screenshot from my storyboard 我试着添加运行时属性,如解释here。但是在它之后,它只改变了最初高亮文本的字体颜色。 我想将我的pickerView和datePicker中的文本颜色更改为白色。我不知道我该如何实现它。如何更改UIPickerView和UIDatePicker的字体颜色?

回答

3

你需要编写此行更改日期选取文字的颜色,

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

您可以使用“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 

希望这会有所帮助。

2

这两行为我节省了一天的时间。

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