在这里检查了很多主题,但找不到解决方案让它发挥作用。
带有属性文字的按钮不会更改其文本颜色。
代码的最终版本:
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.center
let forgotCodeAttributedTitle = NSMutableAttributedString(string: "forgot_code_2_lines".localized(),
attributes: [ kCTParagraphStyleAttributeName as NSAttributedString.Key: style])
forgotCodeAttributedTitle.addAttribute(kCTForegroundColorAttributeName as NSAttributedStringKey, value: Colors.BTN_FORGOT_CODE_TEXT_COLOR, range: NSMakeRange(0, forgotCodeAttributedTitle.string.count))
btnForgotCode.setAttributedTitle(forgotCodeAttributedTitle, for: .normal)
答案 0 :(得分:1)
将其用于NSMutableAttributedString
attributes
参数:
attributes: [NSAttributedStringKey.paragraphStyle: style]
并将你的`addAttribute代码更改为:
let forgotCodeAttributedTitle = NSMutableAttributedString(string: "forgot_code_2_lines", attributes: [NSAttributedStringKey.paragraphStyle: style])
或者,更简单的是,使用此代码在单个语句中设置段落样式和前景色:
let forgotCodeAttributedTitle = NSMutableAttributedString(string: "forgot_code_2_lines", attributes:
[NSAttributedStringKey.paragraphStyle: style,
NSAttributedStringKey.foregroundColor: UIColor.red])