Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. The screenshot from my 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. I am unaware of how can I achieve it.

like image 650
amagain Avatar asked May 17 '17 06:05

amagain


3 Answers

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

And For PickeView, You have to set attributed string like,

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    return NSAttributedString(string: "Your Text", attributes: [NSForegroundColorAttributeName:UIColor.blue])
}
like image 152
Jigar Tarsariya Avatar answered Nov 13 '22 01:11

Jigar Tarsariya


These two lines saved the day for me.

    NepaliDatePicker.setValue(UIColor.white, forKey: "textColor")
    EnglishDatePicker.setValue(UIColor.white, forKey: "textColor")
like image 40
amagain Avatar answered Nov 13 '22 01:11

amagain


You can use "viewForRow" delegate method for "UIPickerView"

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.

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

Hope this helps.

like image 43
Awesome.Apple Avatar answered Nov 13 '22 03:11

Awesome.Apple