【问题标题】:UIPickerView won't allow changing font name and size via delegate's `attributedTitleForRow...`UIPickerView 不允许通过委托的 `attributedTitleForRow...` 更改字体名称和大小
【发布时间】:2015-02-11 20:31:07
【问题描述】:

我使用以下函数来更改字体颜色和字体大小,颜色有效,但字体名称和字体大小拒绝工作。

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? 

var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Arial-BoldMT", size: 45)!, NSForegroundColorAttributeName:UIColor.whiteColor()])

有什么帮助吗?

谢谢。

【问题讨论】:

  • 不只是在 Swift 中...
  • iOS 9 beta 5 上的同样问题
  • 在带有 Swift 4.1 和 Xcode 9.4.1 的 iOS 11.4 中仍然无法使用。也许是时候提交错误了。
  • iOS 12 也不行。
  • iOS 15 签入;还是不行。

标签: cocoa fonts size uipickerview nsattributedstring


【解决方案1】:

我有另一个选择可能对你有帮助..

完成正常选择器视图所需的所有工作:获取更多帮助UIPickerView make in Swift iOS

现在按照这个步骤: - 你可以像这样使用 viewForRow 的方法

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
    var pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.blackColor()
    pickerLabel.text = "PickerView Cell Title"
   // pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
    pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
    pickerLabel.textAlignment = NSTextAlignment.Center
    return pickerLabel
}

更新了 Swift 3

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
    let pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.black
    pickerLabel.text = "PickerView Cell Title"
    // pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
    pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
    pickerLabel.textAlignment = NSTextAlignment.center
    return pickerLabel
}

更新了 Swift 5

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView
    {
        let pickerLabel = UILabel()
        pickerLabel.textColor = UIColor.white
        pickerLabel.text = "PickerView Cell Title"
        // pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
        pickerLabel.font = UIFont.boldSystemFont(ofSize: 20) // In this use your custom font
        pickerLabel.textAlignment = NSTextAlignment.center
        return pickerLabel
    }

可能对你有帮助,我在我的项目中也使用了它。

【讨论】:

  • 是的,您的代码有效。但我需要从 pickerView(pickerView, titleForRow: row, forComponent: component) 中获取重新计算的标题数据。顺便问一下,我如何在评论中发布代码?
  • pickerView(_:titleForRow:forComponent:)
    pickerView(_:attributedTitleForRow:forComponent:)
    pickerView(_:viewForRow:forComponent:reusingView:)
    以上三个也可以显示pickerview的内容,但不要同时使用,我以某种方式使用其中两个。现在只使用最后一个,问题就解决了。
    谢谢大家。
【解决方案2】:

你可以为pickerview声明数据源

let arrDataSource:[String] = ["row 1","row 2","row 3"]

然后使用这个字符串数组作为下面函数中行的标题

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView

{
    let pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.blackColor()
    pickerLabel.text = arrDataSource[row]
     pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 15)
    //pickerLabel.font = UIFont(name: "Arial-BoldMT", size: 15) // In this use your custom font
    pickerLabel.textAlignment = NSTextAlignment.Center
    return pickerLabel
 }

【讨论】:

    【解决方案3】:

    SWIFT 3

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    
        var string = ""
    
        let pickerLabel = UILabel()
        pickerLabel.textColor = UIColor.white
        pickerLabel.text = string
        pickerLabel.font = UIFont(name: "Rubik-Regular", size: 22) // In this use your custom font
        pickerLabel.textAlignment = .center
    
        return pickerLabel
    
    }
    

    【讨论】:

      【解决方案4】:

      要进一步详细说明 https://stackoverflow.com/users/4020910/bennythenerd 对 UILabel 的回答,我会采用更多内存优化的解决方案来重用标签:

      func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
          let pickerLabel: UILabel
          if let label = view as? UILabel {
              pickerLabel = label
          } else {
              pickerLabel = UILabel()
              pickerLabel.font = UIFont(name: "Rubik-Regular", size: 22)
              pickerLabel.textAlignment = .center
          }
          pickerLabel.text = pickerData[row] //This is your string
          return pickerLabel
      }
      

      【讨论】:

      • 这是正确的方法。
      【解决方案5】:

      Swift 4.1 / Xcode 9.4.1

      显然您可以选择自己的字体,但这里我使用的是 System Bold 13。

      func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
          let pickerLabel = UILabel()
          pickerLabel.font = UIFont.boldSystemFont(ofSize: 13)
          pickerLabel.textColor = UIColor.black
          pickerLabel.textAlignment = .center
          pickerLabel.text = "PickerView Cell Title"
      
          return pickerLabel
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-09
        • 1970-01-01
        • 2015-06-26
        • 2011-11-03
        • 1970-01-01
        • 2010-11-15
        • 1970-01-01
        相关资源
        最近更新 更多