Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the UIPickerView text color?

The default UIPickerView color for text is black. There has been some updates to the language in Swift4. I have found my own solution and answered below.

like image 951
Steve B Avatar asked Jan 25 '18 20:01

Steve B


3 Answers

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    return NSAttributedString(string: data[row], attributes: [NSAttributedStringKey.foregroundColor: UIColor.yellow])        
}
like image 163
Steve B Avatar answered Nov 16 '22 16:11

Steve B


You could use:

pickerView.setValue(UIColor.yellow, forKeyPath: "textColor")

This will only change the color for the currently selected row.

See also: https://stackoverflow.com/a/9866686/5306470

like image 26
Matthew Bradshaw Avatar answered Nov 16 '22 16:11

Matthew Bradshaw


Updated for Swift 5:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    return NSAttributedString(string: parksPickerData[row], attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
}
like image 24
Colby Hill Avatar answered Nov 16 '22 15:11

Colby Hill