r.pul
r.pul

Reputation: 129

How do i change the text / font size in pickerview in swift 2.2?

I saw the explanation here in old version of swift.. There must be a different override method and coding that will let me change the text size in swift 2.2 ... Thanks in advance!!

Upvotes: 2

Views: 5173

Answers (1)

Harsh
Harsh

Reputation: 2908

To change the font name and size you can use viewForRow and an attributed string:

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

var label = view as! UILabel!
if label == nil {
    label = UILabel()
}

var data = pickerData[row]
let title = NSAttributedString(string: data!, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(36.0, weight: UIFontWeightRegular)])
label.attributedText = title
label.textAlignment = .Center
return label

}

And if you make your font size larger you'll want to increase the height of each row with rowHeightForComponent:

func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    return 36.0
}

Upvotes: 4

Related Questions