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)) | |
} |