UIPickerView不允许通过委托的`attributionTitleForRow ...`更改字体名称和大小

jdl*_*ung 25 size fonts cocoa uipickerview nsattributedstring

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

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()])
Run Code Online (Sandbox Code Playgroud)

任何帮助?

谢谢.

Jog*_*Com 54

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

完成正常选择器视图所需的所有工作:获取UIPickerView在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
}
Run Code Online (Sandbox Code Playgroud)

更新了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
}
Run Code Online (Sandbox Code Playgroud)

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


小智 9

您可以为pickerview声明数据源

let arrDataSource:[String] = ["row 1","row 2","row 3"]
Run Code Online (Sandbox Code Playgroud)

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

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
 }
Run Code Online (Sandbox Code Playgroud)