[英]How do I change the font color of UIPickerView and UIDatePicker?
I have a viewController
with a UIPickerView
and a UIDatePicker
as shown in the screenshot from my storyboard
as below. 我有一个带有UIPickerView
和UIDatePicker
的viewController
,如下面的storyboard
截图所示。 I tried adding runtime attribute as explained here . 我尝试添加属性运行时所解释这里 。 But following it only changed the font color of the initially highlighted text. 但是它只是改变了最初突出显示的文本的字体颜色。 I want to change the color of text in my both pickerView and datePicker to white. 我想将pickerView和datePicker中的文本颜色更改为白色。 I am unaware of how can I achieve it. 我不知道如何实现它。
you need to write this line to change date picker text color, 你需要写这一行来改变日期选择器文本颜色,
self.datePicker.setValue(UIColor.white, forKeyPath: "textColor")
It will change textcolor of UIDatePicker
它将改变UIDatePicker
textcolor
And For PickeView
, You have to set attributed string like, 对于PickeView
,你必须设置属性字符串,如,
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return NSAttributedString(string: "Your Text", attributes: [NSForegroundColorAttributeName:UIColor.blue])
}
These two lines saved the day for me. 这两行为我节省了一天。
NepaliDatePicker.setValue(UIColor.white, forKey: "textColor")
EnglishDatePicker.setValue(UIColor.white, forKey: "textColor")
You can use "viewForRow" delegate method for "UIPickerView" 您可以为“UIPickerView”使用“viewForRow”委托方法
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
// Your custom view with custom font here
}
For UIDatePicker you can't change font but you can use this code to change textColor. 对于UIDatePicker,您无法更改字体,但可以使用此代码更改textColor。
datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime
Hope this helps. 希望这可以帮助。
You can use attributedTitleForRow delegate method of picker view to change colors. 您可以使用picker视图的attributionTitleForRow委托方法来更改颜色。 refer this link How do I change the color of the text in a UIPickerView under iOS 7? 参考此链接如何在iOS 7下更改UIPickerView中文本的颜色? . 。 You can also use viewForRow delegate method and add a Custom UILabel as the view. 您还可以使用viewForRow委托方法并添加自定义UILabel作为视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.