suisied
suisied

Reputation: 426

How to change UIPickerView font size

How do I change the font size. My code below is not working. I'm using xcode7.1 and swift 2 on iOS 9.1.

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    var attributedString: NSMutableAttributedString
    let myAttribute = [ NSFontAttributeName: UIFont(name: "Chalkduster", size: 13.0)! ]
    switch component {
    case 0:
        attributedString = NSMutableAttributedString(string: firstFieldArray[row], attributes: myAttribute )
    case 1:
        attributedString = NSMutableAttributedString(string: secondFieldArray[row], attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
    default:
        attributedString = NSMutableAttributedString(string: firstFieldArray[row], attributes: myAttribute )
    }

    return attributedString
}

case 1 is working perfectly, while case 0 isn't changing it's fontsize.

Upvotes: 2

Views: 6357

Answers (2)

Maruta
Maruta

Reputation: 1091

Swift 4

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

      var title = UILabel()
         if let view = view {
                title = view as! UILabel
          }
        title.font = UIFont.systemFont(ofSize: 21, weight: UIFont.Weight.bold)
        title.textColor = UIColor.blue
        title.text =  PickerArray[row]
        title.textAlignment = .center

    return title

    }

Upvotes: 4

Quanlong
Quanlong

Reputation: 25526

NSAttributedString cannot be used to change font for UIPickerView.

You can change font by overriding pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView

Upvotes: 11

Related Questions