Swift 4标签属性

蓝色

我正在从swift 3转到swift4。我有UILabel,我为标签提供了非常具体的文本属性。初始化strokeTextAttributes时,出现“在包装可选值时意外发现nil”错误。坦率地说,我完全迷失了。

在swift 3中,strokeTextAttributes的值为[String:Any],但是swift 4引发了错误,直到我将其更改为下面的值。

let strokeTextAttributes = [
    NSAttributedStringKey.strokeColor.rawValue : UIColor.black,
    NSAttributedStringKey.foregroundColor : UIColor.white,
    NSAttributedStringKey.strokeWidth : -2.0,
    NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)
    ] as! [NSAttributedStringKey : Any]


chevronRightLabel.attributedText = NSMutableAttributedString(string: "0", attributes: strokeTextAttributes)
Xavier Lowmiller

@Larme关于不需要的评论.rawValue是正确的。

此外,您可以避免使用显式键入而导致代码崩溃的强制转换:

let strokeTextAttributes: [NSAttributedString.Key: Any] = [
    .strokeColor : UIColor.black,
    .foregroundColor : UIColor.white,
    .strokeWidth : -2.0,
    .font : UIFont.boldSystemFont(ofSize: 18)
]

这也摆脱了重复NSAttributedString.Key.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章