23

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.

Steve B
  • 861
  • 1
  • 7
  • 9

5 Answers5

38
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    return NSAttributedString(string: data[row], attributes: [NSAttributedStringKey.foregroundColor: UIColor.yellow])        
}
Elijah
  • 8,381
  • 2
  • 55
  • 49
Steve B
  • 861
  • 1
  • 7
  • 9
12

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

Matthew Bradshaw
  • 1,843
  • 3
  • 13
  • 21
8

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])
}
Colby Hill
  • 811
  • 1
  • 8
  • 13
7

In storyboard you can add textColor to Key Path:

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
zouritre
  • 231
  • 3
  • 6
4

I have a project with PickerViews and DatePickers (inside a TableViewCell), and I had to use a different sentence for each case.

For PickerViews:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let titleData = "data"
        let myTitle = NSAttributedString(string: titleData, attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightGray])

        return myTitle
    }

For DatePicker:

datePicker.setValue(UIColor.lightGray, forKey: "textColor")
ainareta685
  • 149
  • 1
  • 6