2016-08-15 100 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 
} 
相關問題