Search code examples
iosswift3uipickerviewuidatepicker

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.


Solution

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