0

I use picker view in textfield input view. it works perfectly but when I selected the second row then it changes the color red and then I close picker and when I open second-time picker color of the selected row is not changed but it's not working only for second row .other row works perfectly.

Here is MyCode

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {    
var selectedRow = String()
selectedRow = capicityArray[row]
let pickerLabel = UILabel()

if pickerView.selectedRow(inComponent: component) == row {
            pickerLabel.attributedText = NSAttributedString(string: selectedRow, attributes: [NSAttributedStringKey.font:GlobalFont.Font_GillSansStd(fontsize: 26), NSAttributedStringKey.foregroundColor: GlobalColor.RED_THEME])

        } else {
            pickerLabel.attributedText = NSAttributedString(string: selectedRow, attributes: [NSAttributedStringKey.font:GlobalFont.Font_GillSansStd(fontsize: 22), NSAttributedStringKey.foregroundColor: GlobalColor.Color153])
        }
        pickerLabel.isOpaque = true
        pickerLabel.textAlignment = .center
        return pickerLabel
    }

output :

When First time Selected 2nd row

enter image description here

then close picker and open picker again the enter image description here the second-row not change color red

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184

1 Answers1

0

Use didSelectRow method of UIPickerViewDelegate and change your font color and font size there

UPDATE

I had added this code to selection, to avoid code repeat

func applySelection(pickerView : UIPickerView, row:Int, component:Int) {
        if let selectedView = pickerView.view(forRow: row, forComponent: component)  as? UILabel {
            selectedView.attributedText = NSAttributedString(string: (selectedView.attributedText?.string)!, attributes: [NSAttributedStringKey.font:GlobalFont.Font_GillSansStd(fontsize: 26), NSAttributedStringKey.foregroundColor: GlobalColor.RED_THEME])
        }
    }

Fixing the preselected issue

You can simply add this code in your viewDidAppear or when you show your pickerView

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.applySelection(pickerView: self.pickerView, row: 0, component: 0)
}

Then you can use it like this

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

        let pickerLabel = UILabel()
        pickerLabel.attributedText = NSAttributedString(string: capicityArray[row], attributes: attributes: [NSAttributedStringKey.font:GlobalFont.Font_GillSansStd(fontsize: 22), NSAttributedStringKey.foregroundColor: GlobalColor.Color153])
        pickerLabel.isOpaque = true
        pickerLabel.textAlignment = .center
        return pickerLabel
    }

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.applySelection(pickerView: pickerView, row: row, component: component)
    }
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55