更改UIPickerView的标签

时间:2017-05-24 11:59:26

标签: ios swift uipickerview

我正在尝试更改UIPickerView的字体。

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
    let label = UILabel()

    label.font = UIFont(name:"SynchroLetPlain", size:20)
    //label.font = UIFont.systemFont(ofSize: 17) //Even I use systemFont, it doesn't show nothing
    return label
}

编译没问题,但没有显示文字。

哪里不同?我已经检查过SynchroLetPlain是否包含在项目中。

2 个答案:

答案 0 :(得分:3)

试试这个:

label.font = UIFont(name:"Synchro LET Plain", size:20)

我猜这个问题与字体的实际名称有关。我的意思是iOS搜索字体的实际名称,而不是字体的文件名。

<强>更新

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
    var label: UILabel
    if let view = view as? UILabel {
        label = view
    } else {
        label = UILabel()
        label.font = UIFont(name:"SynchroLetPlain", size:20)
    }
    label.text = "text" // your text
    return label
}

答案 1 :(得分:0)

试试这个

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        var label: UILabel
        if let view = view as? UILabel {
            label = view
        } else {
            label = UILabel()
        }
        label.font = UIFont(name:"SynchroLetPlain", size:20)
        // where data is an Array of String
        label.text = "" // set data here
        return label

    }

另外:

func printFonts()
    {
        for fontFamilyName in UIFont.familyNames {
            print("-- \(fontFamilyName) --", terminator: "")
            for fontName in UIFont.fontNames(forFamilyName: fontFamilyName ) {
                print(fontName)
            }
            print(" ", terminator: "")
        }
    }

调用此函数,它会为您提供用于设置字体的字体的确切名称。