【问题标题】:Label text colour not changing to custom added colour标签文本颜色不会更改为自定义添加的颜色
【发布时间】:2021-08-28 11:31:06
【问题描述】:

我需要向以编程方式添加的标签添加文本颜色。我有一个十六进制颜色,我需要将其分配给我的标签文本。我看不到为文本添加的自定义颜色。我已将相同的代码添加到我的一个视图背景颜色中,它适用于该视图。请帮忙。

注意:self.colorSelected 有十六进制字符串

let labelTextView: UILabel = {
                let labelView = CustomLabelForTextView()
                labelView.translatesAutoresizingMaskIntoConstraints = false
                labelView.font = UIFont(name: "Avenir-Medium", size: 20.0)
                
                if UserDefaults.standard.bool(forKey: "colorSelectedForText") == true {
                    let selectedColor = self.colorSelected
                    let color = UIColor(hex: selectedColor)
                    labelView.textColor = .some(color!)
                }else {
                    labelView.textColor = .darkGray
                }
                labelView.text = “”Hello World
                labelView.backgroundColor = .clear
                return labelView
            }()
      View1.addSubview(labelTextView)
            
            labelTextView.centerXAnchor.constraint(equalTo: View1.centerXAnchor, constant: 0).isActive = true
            labelTextView.centerYAnchor.constraint(equalTo: View1.centerYAnchor, constant: 0).isActive = true
                        labelTextView.widthAnchor.constraint(equalToConstant: 50).isActive = true
                        labelTextView.heightAnchor.constraint(equalToConstant: 30).isActive = true

这是我正在使用的 UIColor 的扩展(在 stackoverflow 上获得)-

extension UIColor {
    public convenience init?(hex: String) {
        let r, g, b, a: CGFloat

        if hex.hasPrefix("#") {
            let start = hex.index(hex.startIndex, offsetBy: 1)
            let hexColor = String(hex[start...])

            if hexColor.count == 8 {
                let scanner = Scanner(string: hexColor)
                var hexNumber: UInt64 = 0

                if scanner.scanHexInt64(&hexNumber) {
                    r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
                    g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
                    b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
                    a = CGFloat(hexNumber & 0x000000ff) / 255

                    self.init(red: r, green: g, blue: b, alpha: a)
                    return
                }
            }
        }

        return nil
    }
}

【问题讨论】:

  • 你确定UserDefaults.standard.bool(forKey: "colorSelectedForText") 是真的吗?尝试添加打印以查看返回前的rg, b, a` 是什么
  • 还可以看看从十六进制获取颜色的“我的看法”:stackoverflow.com/questions/68679658/swift-5-4-hex-to-nscolor/…
  • @PhilipDukhov 是的,值是真的,所有变量都有正确的值,但它仍然没有显示在文本上
  • @PhilipDukhov func hexStringToUIColor(hex:String) -> UIColor {var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() if (cString.hasPrefix("#")) {cString.remove(at: cString.startIndex)} if ((cString.count) != 6) {return UIColor.gray} var rgbValue:UInt64 = 0 Scanner(string: cString).scanHexInt64(&rgbValue) return UIColor(red : CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,绿色: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,蓝色: CGFloat(rgbValue & 0x0000FF) / 255.0,alpha: CGFloat(1.0))}我正在使用此代码将 Hex 转换为 UIColor。还是不行
  • 你能举一个例子来说明你在“selectedColor”中的内容吗?我对您的十六进制代码的简单测试对我不起作用。但是,它适用于我发布的链接中的代码。

标签: ios swift text colors uilabel


【解决方案1】:

只需在主线程中尝试您的颜色设置代码。

DispatchQueue.main.async {
    
    let color = UIColor(hex: selectedColor)
    labelView.textColor = color
    
}

这发生在我在表格视图单元格内设置标签文本颜色时。我通过在主线程中设置文本颜色来解决它。

【讨论】:

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