Swift – How to change UIPickerView font size

swiftuipickerview

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.

Best Answer

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