为什么选择器视图不显示属性字符串?

问题描述 投票:0回答:1

有一个关于堆栈溢出的帖子已经解决了这个问题,但它并没有解决问题,它解决了这个问题。 implemented title attributed for row in picker view did not change the font?

为什么回调pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int)没有显示正确的字体。

这是我的代码:

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

    if component == 0 {  
        print(keysDataSource[row])  
        return keysDataSource[row]  
    } else {  
        print(chordsDataSource[row])  
        return chordsDataSource[row]  
    }  

}  

以下是调试窗口中的结果,显示调用该回调函数时print语句的结果:

vi{
    NSFont = "<UICTFont: 0x113d55700> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 30.00pt";
}

这个问题在屏幕截图中很明显。这些属性并不全都显示出来。显示基线偏移属性,但不显示任何字体属性。它显示了一个系统字体。注意b和L,它们应分别显示为音乐平面符号和音乐缩小符号。

screenshot picker view

ios swift uipickerview nsattributedstring
1个回答
0
投票

丹尼尔布罗尔

使用两种方法标题`原因选择器视图是获得第一个titleForRow。

使用这样的代码。

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
       return pickerViewObj.keys[row]
    }

    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let attributedString = NSAttributedString(string: pickerViewObj.keys[row], attributes: [NSAttributedStringKey.foregroundColor : UIColor.black])
        return attributedString
    }

要么

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
}

我希望这段代码能够正常运行。

© www.soinside.com 2019 - 2024. All rights reserved.