简体   繁体   中英

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

我在旧版本的swift中看到了这里的解释。必须有其他的覆盖方法和编码,这将允许我在swift 2.2中更改文本大小。

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM