在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
如果美工给你的颜色值或者后台取到的颜色值为十六进制时,如: #3492e9
UIColor中没有直接以十六进制色值数为参数的方法,如下可以转为RGB,alpha也可以设定:
1 func colorConversion (Color_Value:NSString, alpha: CGFloat)->UIColor{ 2 var Str :NSString = Color_Value.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString 3 if Color_Value.hasPrefix("#"){ 4 Str=(Color_Value as NSString).substringFromIndex(1) 5 } 6 let redStr = (Str as NSString ).substringToIndex(2) 7 let greenStr = ((Str as NSString).substringFromIndex(2) as NSString).substringToIndex(2) 8 let blueStr = ((Str as NSString).substringFromIndex(4) as NSString).substringToIndex(2) 9 var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0; 10 NSScanner(string:redStr).scanHexInt(&r) 11 NSScanner(string: greenStr).scanHexInt(&g) 12 NSScanner(string: blueStr).scanHexInt(&b) 13 return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: alpha) 14 } 然后调用这个方法来设置颜色就ok了: self.backgroundColor = colorConversion("#3492e9", alpha: 1.0)
|
请发表评论