Swift String to UIColor(16进制字符串转颜色值)

前端之家收集整理的这篇文章主要介绍了Swift String to UIColor(16进制字符串转颜色值)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

func colorWithHexString (hex:String) -> UIColor {

var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
if (cString.hasPrefix("#")) {
cString = cString.substringFromIndex(1)
}
if (countElements(cString) != 6) {
return UIColor.grayColor()
}
var rString = cString.substringToIndex(2)
var gString 2)2)
var bString 4)2)
var r:CUnsignedInt = 0,g:CUnsignedInt = 0,b:CUnsignedInt 0;
NSScanner.scannerWithString(rString).scanHexInt(&r)
.scannerWithString(gString)&g)
.scannerWithString(bString)&b)
return UIColor(red: Float(r) / 255.Float(g) / 255Float(b) Float(1))
}
原文链接:https://www.f2er.com/swift/326507.html

猜你在找的Swift相关文章