2016-08-15 103 views
2

我在老版本的swift中看到了这里的解释.. 必须有一个不同的覆盖方法和编码,这将允许我在swift中更改文本大小2.2 ... 在此先感谢!如何更改swift 2.2中pickerview中的文本/字体大小?

+0

的可能的复制[UIPickerView不会允许通过委托的\'attributedTitleForRow ... \'更改字体名称和大小(http://stackoverflow.com/questions/27455345/uipickerview-wont-allow-changing -font-名称和大小通代表-attributedt) – penatheboss

回答

4

要更改字体名称和大小,您可以使用viewForRow和属性串:

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 

} 

如果你让你的字体大小,你会想增加与rowHeightForComponent每一行的高度:

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