ios - 在选择器 View 中为行实现的标题没有改变字体?

标签 ios swift uipickerview

我尝试更改选择器 View 中标题的字体,但由于某种原因我不能。我可以更改标题的颜色,但字体保持不变?

 func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let attrTitle = NSAttributedString(string: splitPickerSource[row], attributes: [NSFontAttributeName: UIFont(name: "Avenir-Heavy", size: 17.0)!, NSForegroundColorAttributeName: UIColor.darkGray])
        return attrTitle
    }

或者,我可以为 UIPickerView 实现 viewForRow 委托(delegate)方法,并从那里添加新标签。

尝试打印 attrTitle 时 maddy 建议的一些调试消息

Share Bill With{ NSColor = "UIExtendedSRGBColorSpace 0.117647 0.117647 0.117647 1"; NSFont = "<UICTFont: 0x7fb5f8d14080> font-family: \"Avenir-Heavy\"; font-weight: bold; font-style: normal; font-size: 14.00pt";

任何人都对这种情况有任何想法,为什么我不能修改字体 attributedTitleForRow 委托(delegate)方法?

最佳答案

您输入错误,这就是它不起作用的原因。应该是

    let attrTitle = NSAttributedString(string: splitPickerSource[row], attributes: [NSFontAttributeName: UIFont(name: "Avenir-Heavy", size: 17.0)!, NSForegroundColorAttributeName: UIColor.darkGray]) 

在 NSFontAttributeName 应该结束后,我删除了额外的 size: 14。

关于ios - 在选择器 View 中为行实现的标题没有改变字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40876229/

相关文章:

ios - NSPredicate 不适用于计算字段

ios - 以编程方式设置 RoundedRect 按钮

ios - 如何在 NSManagedObject 子类中定义 Fetched 属性

ios - 在 iPhone 中获取 WIFI 信号强度

ios - 在 iOS 上静音 JWPlayer (Swift)

iphone - 在 UIPickerView 选择指示器上显示一个单元?

ios - Swift-如何在App Store中共享应用程序链接

c# - 如何确定 macOS 应用程序是否是通过 url 启动的

iphone - 当点击另一个 UITextField 调出选择器时,键盘不会关闭

ios - UIPickerView 随机旋转 UILabel iOS