amagain
amagain

Reputation: 2072

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.

Upvotes: 6

Views: 5208

Answers (4)

amagain
amagain

Reputation: 2072

These two lines saved the day for me.

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

Upvotes: 2

Jigar Tarsariya
Jigar Tarsariya

Reputation: 3247

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

Upvotes: 14

Awesome.Apple
Awesome.Apple

Reputation: 1324

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.

Upvotes: 1

jegadeesh
jegadeesh

Reputation: 945

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.

Upvotes: -1

Related Questions