【问题标题】:How to change UIPickerView font size如何更改 UIPickerView 字体大小
【发布时间】:2015-12-09 07:32:10
【问题描述】:

如何更改字体大小。我下面的代码不起作用。我在 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 并没有改变它的字体大小。

【问题讨论】:

    标签: swift uipickerview


    【解决方案1】:

    NSAttributedString 不能用于更改 UIPickerView 的字体。

    您可以通过覆盖pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView来更改字体

    【讨论】:

    • 是的,我已经做了这个解决方案,它是正确的。此外,您应该在此视图中添加一个 UILabel 并在其中设置您的 UIFont。
    【解决方案2】:

    斯威夫特 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
    
        }
    

    【讨论】:

    • Getting... 无法赋值,title 是一个 let 常量
    猜你喜欢
    • 2011-11-03
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 2019-02-19
    • 2011-09-24
    相关资源
    最近更新 更多