您的位置:首页 > 移动开发 > Swift

swift-3.0 将HexColor转变为UIColor的方法

2017-01-06 17:31 363 查看
swift-3.0 将HexColor转变为UIColor的方法

// MARK:- 把#ffffff颜色转为UIColor

extension UIColor {

class func colorWithHexString(hex:String) ->UIColor {

var cString = hex.trimmingCharacters(in:CharacterSet.whitespacesAndNewlines).uppercased()

if (cString.hasPrefix("#")) {

let index = cString.index(cString.startIndex, offsetBy:1)

cString = cString.substring(from: index)

}

if (cString.characters.count != 6) {

return UIColor.red

}

let rIndex = cString.index(cString.startIndex, offsetBy: 2)

let rString = cString.substring(to: rIndex)

let otherString = cString.substring(from: rIndex)

let gIndex = otherString.index(otherString.startIndex, offsetBy: 2)

let gString = otherString.substring(to: gIndex)

let bIndex = cString.index(cString.endIndex, offsetBy: -2)

let bString = cString.substring(from: bIndex)

var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;

Scanner(string: rString).scanHexInt32(&r)

Scanner(string: gString).scanHexInt32(&g)

Scanner(string: bString).scanHexInt32(&b)

return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: