Change selected row label color in picker view swift 1.2

后端 未结 1 1827
说谎
说谎 2021-01-25 17:17

I have a created the pickerview with the following code

    @IBOutlet var myPicker: UIPickerView!

    var colors: [String] = [\"red\",\"green\",\"blue\"]

    o         


        
相关标签:
1条回答
  • 2021-01-25 17:50

    you could do something like the following:

    class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    
      var colors = ["red","green","blue"]
    
      func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
      }
    
      func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return colors.count
      }
    
      func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let color = (row == pickerView.selectedRowInComponent(component)) ? UIColor.orangeColor() : UIColor.blackColor()
        return NSAttributedString(string: colors[row], attributes: [NSForegroundColorAttributeName: color])
      }
    
      func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        pickerView.reloadAllComponents()
      }
    }
    
    0 讨论(0)
提交回复
热议问题