Change UIPicker color

北慕城南 提交于 2019-12-07 03:38:57

问题


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:


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
}



回答2:


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
}



回答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.




回答4:


*For both Date picker and Normal Picker

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



回答5:


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
    }

}


来源:https://stackoverflow.com/questions/29243564/change-uipicker-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!