iOS开发

iOS 十六进制字符串转颜色 已更新到Swift4.0语法

2017-09-18  本文已影响44人  seasonZhu

最近一直都在做Swift的项目,不过UI基本上都是使用的一套,里面备注的颜色信息都是使用CSS的来表示的.
安卓的兄弟用CSS写起来比较方便,为了保证开发同步进行,我用Swift中写了颜色的分类,可以直接将字符串转为颜色.

后来才知道CSS表示也是有带透明度和不带透明度的
比如#ffffff #号后面为6位就是每2位代表R\G\B
还有就是#1a000000 #号后的前2位 代表的是透明度, 后面的6位每2位代表R\G\B

于是我写了两个分类方法,其实通过字符串长度判断,进行方法的封装的话,写一个方法就够了,不过我这样表达也算比较清楚

最近Swift4.0发布了,之前截取字符串的方法已经过期了,我更新到了最新的语法,被注释的地方是原有的语法, 希望对大家进行语法更改也有帮助

告警如下:

'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.
'substring(with:)' is deprecated: Please use String slicing subscript.

大家可以借鉴一下.

extension UIColor {
    
    
    /// 字符串转颜色
    ///
    /// - Parameter colorStr: 字符串,如#FFFFFF 或者 如#1a000000
    /// - Returns: 返回颜色
    class func CSSHex(_ colorStr: String) -> UIColor {
        
        var cStr : String = colorStr.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        
        if cStr.hasPrefix("#") {
            let index = cStr.index(after: cStr.startIndex)
            //cStr = cStr.substring(from: index)
            cStr = String(cStr[index...])
        }
        
        if cStr.characters.count == 6 {
            return color(cStr: cStr)
        }else if cStr.characters.count == 8 {
            return colorWithAlpha(cStr: cStr)
        }else {
            return UIColor.black
        }
    }
    
    
    /// 6位字符串转颜色
    ///
    /// - Parameter cStr: 6位字符串转没有涉及透明度的颜色
    /// - Returns: 返回颜色
    private class func color(cStr: String) -> UIColor {
        var color = UIColor.white
        
        let rRange = cStr.startIndex ..< cStr.index(cStr.startIndex, offsetBy: 2)
        //let rStr = cStr.substring(with: rRange)
        let rStr = String(cStr[rRange])
        
        let gRange = cStr.index(cStr.startIndex, offsetBy: 2) ..< cStr.index(cStr.startIndex, offsetBy: 4)
        //let gStr = cStr.substring(with: gRange)
        let gStr = String(cStr[gRange])
        
        let bIndex = cStr.index(cStr.endIndex, offsetBy: -2)
        //let bStr = cStr.substring(from: bIndex)
        let bStr = String(cStr[bIndex...])
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0
        Scanner(string: rStr).scanHexInt32(&r)
        Scanner(string: gStr).scanHexInt32(&g)
        Scanner(string: bStr).scanHexInt32(&b)
        
        color = UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
        
        return color
    }
    
    /// 8位字符串转颜色
    ///
    /// - Parameter cStr: 8位字符串转涉及透明度的颜色
    /// - Returns: 返回颜色
    private class func colorWithAlpha(cStr: String) -> UIColor {
        var color = UIColor.white
        
        let aRange = cStr.startIndex ..< cStr.index(cStr.startIndex, offsetBy: 2)
        let aStr = String(cStr[aRange])
        
        let rRange = cStr.index(cStr.startIndex, offsetBy: 2) ..< cStr.index(cStr.startIndex, offsetBy: 4)
        let rStr = String(cStr[rRange])
        
        let gRange = cStr.index(cStr.startIndex, offsetBy: 4) ..< cStr.index(cStr.startIndex, offsetBy: 6)
        let gStr = String(cStr[gRange])
        
        let bIndex = cStr.index(cStr.endIndex, offsetBy: -2)
        let bStr = String(cStr[bIndex...])
        
        var a:CUnsignedInt = 0, r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0
        Scanner(string: aStr).scanHexInt32(&a)
        Scanner(string: rStr).scanHexInt32(&r)
        Scanner(string: gStr).scanHexInt32(&g)
        Scanner(string: bStr).scanHexInt32(&b)
        
        color = UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(a) / 255.0)
        
        return color
    }
}
上一篇下一篇

猜你喜欢

热点阅读