美文网首页
swift color适配 颜色适配

swift color适配 颜色适配

作者: uiari | 来源:发表于2020-05-31 21:30 被阅读0次

使用

        self.view.backgroundColor = UIColor(normal: "ffffff") //黑夜模式与正常颜色通用 透明度 1
        self.view.backgroundColor = UIColor(normal: "ffffff", dark: "000000") //黑夜模式下 黑色  正常:白色 透明度 1
        self.view.backgroundColor = UIColor(normal: "ffffff", dark: "000000", alpha: 0.5) //黑夜模式下 黑色  正常:白色 透明度均为 0.5
        self.view.backgroundColor = UIColor(normal: "ffffff", normalAlpha: 0.5, dark: "000000", darkAalpha: 1) //黑夜模式下 黑色  正常:白色  透明度 黑夜:1  正常:0.5

代码 UIColor 分类

    convenience init(hexString: String, alpha: CGFloat) {
        var hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
         
        if hexString.hasPrefix("#") {
            hexString = hexString.substring(from: 1)
        }
        else if hexString.hasPrefix("0X") {
            hexString = hexString.substring(from: 2)
        }
        var red: Float = 0
        var green: Float = 0
        var blue: Float = 0
        var a: Float = 0
        
        let length = hexString.count
        if (length != 3 && length != 4 && length != 6 && length != 8) {
            self.init()
        }
        
        if length<5 {
            Scanner(string: String(format: "%@%@", "0x",hexString.subString(rang: NSRange(location: 0, length: 1)))).scanHexFloat(&red)
            Scanner(string: String(format: "%@%@", "0x",hexString.subString(rang: NSRange(location: 1, length: 1)))).scanHexFloat(&green)
            Scanner(string: String(format: "%@%@", "0x",hexString.subString(rang: NSRange(location: 2, length: 1)))).scanHexFloat(&blue)
            if length == 4 {
                Scanner(string: String(format: "%@%@", "0x",hexString.subString(rang: NSRange(location: 3, length: 1)))).scanHexFloat(&a)
            }
            else {
                a = 1
            }
        }
        else {
            Scanner(string: String(format: "%@%@", "0x",hexString.subString(rang: NSRange(location: 0, length: 2)))).scanHexFloat(&red)
            Scanner(string: String(format: "%@%@", "0x",hexString.subString(rang: NSRange(location: 2, length: 2)))).scanHexFloat(&green)
            Scanner(string: String(format: "%@%@", "0x",hexString.subString(rang: NSRange(location: 4, length: 2)))).scanHexFloat(&blue)
            if length == 8 {
                Scanner(string: String(format: "%@%@", "0x",hexString.subString(rang: NSRange(location: 6, length: 2)))).scanHexFloat(&a)
            }
            else {
                a = 255.0
            }
        }
        if length == 4 || length == 8 {
        }
        else {
            a = min(Float(alpha*255.0), 255.0)
        }
        self.init(red: CGFloat(red/255.0), green: CGFloat(green/255.0), blue: CGFloat(blue/255.0), alpha: CGFloat(a/255.0))
    }

    convenience init(normal: String) {
        self.init(normal: normal, dark: "")
    }
    
    convenience init(normal: String, dark: String) {
        self.init(normal: normal, dark: dark, alpha: 1)
    }
    
    convenience init(normal: String, dark: String, alpha: CGFloat) {
        
        self.init(normal: normal, normalAlpha: alpha, dark: dark, darkAalpha: alpha)
    }
    
    convenience init(normal: String, normalAlpha: CGFloat, dark: String, darkAalpha: CGFloat) {
        if dark.count == 0 {
            self.init(hexString:normal, alpha:normalAlpha)
        }
        else {
            if #available(iOS 13.0, *) {
                self.init { (traitCollection: UITraitCollection) -> UIColor in
                    if (traitCollection.userInterfaceStyle == .dark) {
                        return UIColor(hexString: dark, alpha: darkAalpha)
                    }
                    else {
                        return UIColor(hexString: normal, alpha: normalAlpha)
                    }
                };
            } else {
                self.init(hexString:normal, alpha: normalAlpha)
            }
        }
    }

相关文章

网友评论

      本文标题:swift color适配 颜色适配

      本文链接:https://www.haomeiwen.com/subject/ukorzhtx.html