7

How can I change the color of my picker?

I don't wish to change the font or the text. My picker is populate and works exactly how I want it, what I want to do is change the color from black to my custom white.

1
  • Change the color of what : the font, the background, the selected item, the unselected items? Commented Mar 24, 2015 at 21:54

5 Answers 5

18

Take a look at: http://makeapppie.com/tag/uipickerview-in-swift/

If you want to change your title color of each element you could implement:

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let titleData = pickerData[row]
    var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
    return myTitle
}

Swift 3:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let titleData = pickerData[row]
    let myTitle = NSAttributedString(string: titleData!, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.white])
    return myTitle
}
Sign up to request clarification or add additional context in comments.

1 Comment

This did exactly what I needed and enabled me to use white text in a picker view on a black background. Thank you
5

*For both Date picker and Normal Picker

   override func viewDidLoad() {
   super.viewDidLoad()
   pickerView.setValue(UIColor.blue, forKey: "textColor")
   pickerView.setValue(UIColor.black, forKey: "backgroundColor")
 }

Comments

4

Swift 4.0

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let titleData = arrayOfPickerData[row]
    let myTitle = NSAttributedString(string: titleData, attributes: [.font:UIFont(name: "Georgia", size: 15.0)!, .foregroundColor:UIColor.white]))
    return myTitle
}

Comments

3

If you want more control customizing each element in the picker...

Use UIPickerViewDelegate's "viewForRow" method.

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

    var label: UILabel
    if let view = view as? UILabel { label = view }
    else { label = UILabel() }

    label.textColor = UIColor.blue
    label.textAlignment = .center
    label.font = UIFont(name: "My-Custom-Font", size: 20) // or UIFont.boldSystemFont(ofSize: 20)
    label.adjustsFontSizeToFitWidth = true
    label.minimumScaleFactor = 0.5
    label.text = getTextForPicker(atRow: row) // implemented elsewhere

    return label
}

Obviously the view you're returning could be more complex than just a UILabel.

Comments

0

Swift 4.0 with Multiple Components in UIPickerView

Each "component" is the list that show in the UIPickerView that you design. For example if you have the something like the following: A list of names in an array and another list of ages in an array and both are displayed such that you pick a persons name and their age. The first component 'names' in forComponent is component == 0; The second component 'age' in forComponent is componennt == 1; and so on based on the number of components you have listed.

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

    if component == 0 {
        let nameData = namesArray[row]
        let myNameData = NSAttributedString(string: nameData, attributes: [.foregroundColor:UIColor.white])
        return myNameData
    } else {
        let ageData = ageArray[row]
        let myAgeData = NSAttributedString(string: ageData, attributes: [.foregroundColor:UIColor.white])
        return myAgeData
    }

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.