如何在 Swift 中比较 UIColors



我正在尝试比较 Swift 中的不同颜色。一切都很好,直到我重新构建应用程序。相同的颜色,相同的代码,但它不起作用。

正在为文本添加背景颜色,并且我有不同的主题。当用户更改主题时,应用还应更改该主题的背景颜色。使用该应用程序时一切都很好,但是当我重建项目时,它不会像应有的那样比较颜色。

这是我正在使用的代码:

for i in 0...noteTextView.attributedText.length-1
        {
            noteTextView.attributedText.enumerateAttribute(NSBackgroundColorAttributeName, in: NSMakeRange(i,1), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) -> Void in
                    if let exValue = value as? UIColor
                    {
                        if(exValue.isEqualWithConversion(UIColor(hex: 0x2e3f4f))) || (exValue.isEqualWithConversion(UIColor(hex: 0xc0e1ff))) || (exValue.isEqualWithConversion(UIColor(hex: 0x2970ae))) || (exValue.isEqualWithConversion(UIColor(hex: 0xaad5fb)))
                        {
                            aRange = NSMakeRange(i, 1)
                            print("blue")
                            myValue = styles.blueHighlightColor()
                            self.noteTextView.textStorage.addAttribute(attributeName, value: myValue, range: aRange)
                            saveChanges = true
                        }
                        else if (exValue.isEqualWithConversion(UIColor(hex: 0xcafd70))) || (exValue.isEqualWithConversion(UIColor(hex: 0xbde970))) || (exValue.isEqualWithConversion(UIColor(hex: 0x568a56))) || (exValue.isEqualWithConversion(UIColor(hex: 0xbde970)))
                        {
                            aRange = NSMakeRange(i, 1)
                            print("green")
                            myValue = styles.greenHighlightColor()
                            self.noteTextView.textStorage.addAttribute(attributeName, value: myValue, range: aRange)
                            saveChanges = true
                        }
                        else if (exValue.isEqualWithConversion(UIColor(hex: 0x51445d))) || (exValue.isEqualWithConversion(UIColor(hex: 0xd6affb))) || (exValue.isEqualWithConversion(UIColor(hex: 0x79569b))) || (exValue.isEqualWithConversion(UIColor(hex: 0xd6affb)))
                        {
                            aRange = NSMakeRange(i, 1)
                            print("pink")
                            myValue = styles.pinkHighlightColor()
                            self.noteTextView.textStorage.addAttribute(attributeName, value: myValue, range: aRange)
                            saveChanges = true
                        }
                        else if (exValue.isEqualWithConversion(UIColor(hex: 0xffffff))) || (exValue.isEqualWithConversion(UIColor(hex: 0xfbe769))) || (exValue.isEqualWithConversion(UIColor(hex: 0x77745f))) || (exValue.isEqualWithConversion(UIColor(hex: 0xfbe769)))
                        {
                            aRange = NSMakeRange(i, 1)
                            print("yellow")
                            myValue = styles.yellowHighlightColor()
                            self.noteTextView.textStorage.addAttribute(attributeName, value: myValue, range: aRange)
                            saveChanges = true
                        }
                    }
                }
        } // For loop end

我尝试使用"=="以及此扩展:

    extension UIColor
{
    func isEqualWithConversion(_ color: UIColor) -> Bool {
        guard let space = self.cgColor.colorSpace
            else { return false }
        guard let converted = color.cgColor.converted(to: space, intent: .absoluteColorimetric, options: nil)
            else { return false }
        return self.cgColor == converted
    }
}

此外,如果我使用标准颜色,如 UIColor.red 或 .blue 或 .green 等,它可以正常工作。

我需要

的解决方案:

 func compareColors (c1:UIColor, c2:UIColor) -> Bool{
    // some kind of weird rounding made the colors unequal so had to compare like this
    var red:CGFloat = 0
    var green:CGFloat  = 0
    var blue:CGFloat = 0
    var alpha:CGFloat  = 0
    c1.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
    var red2:CGFloat = 0
    var green2:CGFloat  = 0
    var blue2:CGFloat = 0
    var alpha2:CGFloat  = 0
    c2.getRed(&red2, green: &green2, blue: &blue2, alpha: &alpha2)
    return return (Int(red*255) == Int(red*255) && Int(green*255) == Int(green2*255) && Int(blue*255) == Int(blue*255) )

}

我发现比较UIColors的最简单方法是将它们转换为CGColors,因为CoreGraphics有一个名为 CGColorEqualToColor 的方法。供您比较使用

if(CGColorEqualToColor(Exvalue.CGColor, UIColor(hex: 0x2e3f4f).CGColor){
    //code stuffs
}

我无法检查语法是否 100% 正确(我目前不在计算机上(,但您应该能够在 xCode 中弄清楚这一点。 如果您有任何其他问题,请告诉我。

这是我用来获取 RGB 值的 UIColor 扩展,也许它会有所帮助:

extension UIColor {
    func rgb() -> (Int?, Int?, Int?) {
        var fRed : CGFloat = 0
        var fGreen : CGFloat = 0
        var fBlue : CGFloat = 0
        var fAlpha: CGFloat = 0
        if self.getRed(&fRed, green: &fGreen, blue: &fBlue, alpha: &fAlpha) {
            let iRed = Int(fRed * 255.0)
            let iGreen = Int(fGreen * 255.0)
            let iBlue = Int(fBlue * 255.0)
            return (iRed, iGreen, iBlue)
        } else {
            // Could not extract RGBA components:
            return (nil,nil,nil)
        }
    }
}

用法:

let newTheme = UIColor(colorLiteralRed: 30.2, green: 46.3, blue: 55.6, alpha: 1.0)
let newThemeRgb = newTheme.rgb()
let currentTheme = myView.backgroundColor
let currentThemeRgb = currentTheme.rgb()
        glClearColor(Float(rgb.0!)/256.0, Float(rgb.1!)/256.0, Float(rgb.2!)/256.0, 0.0);
if newThemeRgb.0 <> currentThemeRgb.0 || newThemeRgb.1 <> currentTheme.1 || newThemeRgb.2 <> currentThemeRgb.2 {
    //change view color
    myView.backgroundColor = newTheme
}

相关内容

  • 没有找到相关文章

最新更新