【发布时间】:2020-12-02 19:00:31
【问题描述】:
橙色在具有 iOS14 的 iphoneXR(设备)上显示良好,并且在深色和浅色模式之间保持橙色。但是在带有 iOS13(设备)的 iphone 11 上,标题颜色仅显示为黑色或白色,具体取决于暗/亮模式。 为什么要覆盖橙色?我该怎么做才能让所有设备的标题都保持橙色?
iphone 11 和 iOS13 截图:
iphone XR 与 iOS14.2 截图:
使用 XCode 版本 12
我试过了:
button.setTitleColor(.systemOrange, for: .normal)
button.setTitleColor(.placeholderText, for: .normal) //this led to a grey color on ios14 as expected but black on iOS13
button.setTitleColor(UIColor.orange, for: .normal)
button.setTitleColor(UIColor.customAccent, for: .normal)
extension UIColor {
static var customAccent: UIColor {
if #available(iOS 13, *) {
return UIColor { (traitCollection: UITraitCollection) -> UIColor in
if traitCollection.userInterfaceStyle == .dark {
return UIColor(red: 0.8196078431, green: 0.3568627451, blue: 0.2235294118, alpha: 1.0)
} else {
return UIColor(red: 0.8196078431, green: 0.3568627451, blue: 0.2235294118, alpha: 1.0)
}
}
} else {
return UIColor(red: 0.8196078431, green: 0.3568627451, blue: 0.2235294118, alpha: 1.0)
}
}
按钮以编程方式设置:
let attributedText = [NSAttributedString.Key.font: UIFont(name: "Poppins-Regular", size: 15.0)!]
button1.layer.borderColor = #colorLiteral(red: 0.8196078431, green: 0.3568627451, blue: 0.2235294118, alpha: 1)
button1.layer.borderWidth = 1.0
button1.setTitleColor(.placeholderText, for: .normal)
button1.backgroundColor = UIColor.white
button1.layer.cornerRadius = 32
button1.clipsToBounds = true
button1.setAttributedTitle(NSAttributedString(string: "PLACEHOLDER TEXT", attributes: attributedText), for: .normal)
button1.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(button1)
self.addConstraint(NSLayoutConstraint(item: button1, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0, constant: 64))
self.addConstraint(NSLayoutConstraint(item: button1, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 45))
self.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: button1, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 45))
self.addConstraint(NSLayoutConstraint(item: button1, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: button1, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: reScanButton!, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 15))
【问题讨论】:
-
我不会在 UILabel 和
UIButton中混合使用.attributedText和.text/.textColor。我会用正确颜色的标题重做setAttributedTitle(_:for:)。 -
即:
let attributedTitle = NSMutableAttributedString(attributedString:; button.attributedTitle(for: state)!); attributedTitle.setAttributes([.foregroundColor: someColor], range: _NSRange(location: 0, length: attributedTitle.length)); button.setAttributedTitle(attributedTitle, for: state) -
谢谢!属性文本是问题所在,添加 .foregroundColor 作为键使其工作。
标签: swift xcode uibutton ios13 uicolor