【问题标题】:iOS13 - UIButton title color only showing black or white instead of in orangeiOS13 - UIButton 标题颜色仅显示黑色或白色而不是橙色
【发布时间】: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


【解决方案1】:

我认为这是因为您为同一状态设置了两种不同的颜色。

尝试改变按钮的状态。

button.setTitleColor(UIColor.orange, for: .normal)
button.setTitleColor(UIColor.customAccent, for: .touchUpInside)

您可以在此处阅读文档:https://developer.apple.com/documentation/uikit/uicontrol/state

【讨论】:

    【解决方案2】:

    如果您有UILabel,您通常不想混合属性,因为您可能会得到奇怪/混合的结果,正如您所看到的:

    attributedText
    vs
    textfonttextColortexAlignmentlineBreakMode

    UITextViewUIButton 也是如此(引擎盖下有一个 UILabel)。

    所以继续使用NSAttributedString,例如:

    let state: UIControl.State = .normal
    guard let currentAttributedTitle = button.attributedTitle(for: state) else { return }
    let attributedTitle = NSMutableAttributedString(attributedString: currentAttributedTitle)
    attributedTitle.setAttributes([.foregroundColor: color], range: NSRange(location: 0, length: attributedTitle.length))
    button.setAttributedTitle(attributedTitle, for: state)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      相关资源
      最近更新 更多