关于Dark模式颜色适配的一个简易方案

2020-03-19  本文已影响0人  蹦擦擦我勒个去
   由于苹果爸爸的一贯政策,否管你多牛,强如微信也不得不低头,app的dark模式也就不得不做了,但是苹果官方的app其dark模式的UI设计在颜色处理方面的工作量几乎是double,这样对设计者和开发者来说都是一种额外的负担。
   废话不多说,直接来方案:
extension UIColor {
    static func dynamicColor(from hexString: String) -> UIColor? {
        var hex = ""
        if hexString.lowercased().hasPrefix("0x") {
            hex =  hexString.replacingOccurrences(of: "0x", with: "")
        } else if hexString.hasPrefix("#") {
            hex = hexString.replacingOccurrences(of: "#", with: "")
        } else {
            hex = hexString
        }
        var transparency: CGFloat = 1
        if hex.count == 8 {
            guard let hexValue = Int(hex, radix: 16) else { return nil }
            transparency = CGFloat(hexValue & 0xff) / 255.0
            hex = (hexString as NSString).substring(to: 6)
        }
        var res = [String]()
        for char in hex.charactersArray {
            guard let dv = Int(String(char), radix: 16) else { return nil }
            res.append(String(format: "%X", 15 - dv))
        }
        let darkHex = res.joined()
        
        if #available(iOS 13.0, *) {
            return UIColor { (trait) -> UIColor in
                if trait.userInterfaceStyle == .dark {
                    return UIColor(hexString: darkHex, transparency: transparency) ?? .white
                }
                return UIColor(hexString: hex, transparency: transparency) ?? .white
            }
        } else {
            return UIColor(hexString: hex, transparency: transparency)
        }
    }
    
    convenience init?(hexString: String, transparency: CGFloat = 1) {
        var string = ""
        if hexString.lowercased().hasPrefix("0x") {
            string =  hexString.replacingOccurrences(of: "0x", with: "")
        } else if hexString.hasPrefix("#") {
            string = hexString.replacingOccurrences(of: "#", with: "")
        } else {
            string = hexString
        }

        if string.count == 3 { // convert hex to 6 digit format if in short format
            var str = ""
            string.forEach { str.append(String(repeating: String($0), count: 2)) }
            string = str
        }

        guard let hexValue = Int(string, radix: 16) else { return nil }

        var trans = transparency
        if trans < 0 { trans = 0 }
        if trans > 1 { trans = 1 }

        let red = (hexValue >> 16) & 0xff
        let green = (hexValue >> 8) & 0xff
        let blue = hexValue & 0xff
        self.init(red: red, green: green, blue: blue, transparency: trans)
    }
    
    convenience init?(red: Int, green: Int, blue: Int, transparency: CGFloat = 1) {
        guard red >= 0 && red <= 255 else { return nil }
        guard green >= 0 && green <= 255 else { return nil }
        guard blue >= 0 && blue <= 255 else { return nil }

        var trans = transparency
        if trans < 0 { trans = 0 }
        if trans > 1 { trans = 1 }

        self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: trans)
    }
}

以上是在Swift上的实现,如有需要可以移植到所需的平台。
上一篇下一篇

猜你喜欢

热点阅读