6

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.

Community
  • 1
  • 1
amagain
  • 2,042
  • 2
  • 20
  • 36

4 Answers4

14

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])
}
Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
  • 2
    Thanks very much. It's pathetic that Apple won't just provide a simple ```setTextColor``` method for this. I can't believe they think it's acceptable to only have black font.... – Supertecnoboff Jun 24 '18 at 20:55
2

These two lines saved the day for me.

    NepaliDatePicker.setValue(UIColor.white, forKey: "textColor")
    EnglishDatePicker.setValue(UIColor.white, forKey: "textColor")
amagain
  • 2,042
  • 2
  • 20
  • 36
1

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.

Awesome.Apple
  • 1,316
  • 1
  • 11
  • 24
-1

You can use attributedTitleForRow delegate method of picker view to change colors. refer this link How do I change the color of the text in a UIPickerView under iOS 7?. You can also use viewForRow delegate method and add a Custom UILabel as the view.

Community
  • 1
  • 1
jegadeesh
  • 875
  • 9
  • 22