如何更改 UIPickerView 字体大小
How to change UIPickerView font size
如何更改字体大小。我下面的代码不起作用。我在 iOS 9.1.
上使用 xcode7.1 和 swift 2
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
}
案例 1 工作正常,而案例 0 没有改变它的字体大小。
NSAttributedString
不能用于更改 UIPickerView 的字体。
您可以通过覆盖 pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView
来更改字体
Swift 4
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var title = UILabel()
if let view = view {
title = view as! UILabel
}
title.font = UIFont.systemFont(ofSize: 21, weight: UIFont.Weight.bold)
title.textColor = UIColor.blue
title.text = PickerArray[row]
title.textAlignment = .center
return title
}
如何更改字体大小。我下面的代码不起作用。我在 iOS 9.1.
上使用 xcode7.1 和 swift 2func 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
}
案例 1 工作正常,而案例 0 没有改变它的字体大小。
NSAttributedString
不能用于更改 UIPickerView 的字体。
您可以通过覆盖 pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView
Swift 4
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var title = UILabel()
if let view = view {
title = view as! UILabel
}
title.font = UIFont.systemFont(ofSize: 21, weight: UIFont.Weight.bold)
title.textColor = UIColor.blue
title.text = PickerArray[row]
title.textAlignment = .center
return title
}