gpt4 book ai didi

ios - 为什么选择器 View 不显示属性字符串?

转载 作者:行者123 更新时间:2023-11-29 05:51:17 26 4
gpt4 key购买 nike

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

为什么回调 pickerView(_ pickerView: UIPickerView, attributeTitleForRow 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]
}

}

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

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

问题在屏幕截图中很明显。属性并未全部显示。显示基线偏移属性,但不显示任何字体属性。它显示的是系统字体。注意 b 和 L,它们应该分别显示为音乐降号符号和音乐减号符号。

screenshot picker view

最佳答案

丹尼尔·布劳尔

对标题使用这两种方法`因为选择器 View 首先获取 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
}

我希望这段代码能够工作。

关于ios - 为什么选择器 View 不显示属性字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55650114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com