这个算是常用的函数了 网上的都是swift2版本 自己更新了下在这里留个档
func colorWithHexString (hex:String) -> UIcolor { var cString:String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() if (cString.hasPrefix("#")) { cString = (cString as Nsstring).substring(from: 1) } if (cString.characters.count == 3){ cString += cString } let rString = (cString as Nsstring).substring(to: 2) let gString = ((cString as Nsstring).substring(from: 2) as Nsstring).substring(to: 2) let bString = ((cString as Nsstring).substring(from: 4) as Nsstring).substring(to: 2) 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)) }func CGcolorFromrGB(rgbValue: UInt) -> CGcolor { return UIcolor( red: CGfloat((rgbValue & 0xFF0000) >> 16) / 255.0,green: CGfloat((rgbValue & 0x00FF00) >> 8) / 255.0,blue: CGfloat(rgbValue & 0x0000FF) / 255.0,Alpha: CGfloat(1.0) ).cgcolor }总结
以上是内存溢出为你收集整理的Swift3 十六进制颜色代码转UIColor & CGColor全部内容,希望文章能够帮你解决Swift3 十六进制颜色代码转UIColor & CGColor所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)