import UIKit | |
extension UIcolor { | |
public convenIEnce init(rgba: String) { | |
var red: CGfloat = 0.0 | |
var green: CGfloat 0 | |
var blue: CGfloat 0 | |
var Alpha: CGfloat 10 | |
if rgba.hasPrefix("#") { | |
let index = advance(rgba.startIndex,1) | |
let hex = rgba.substringFromIndex(index) | |
let scanner = NSScanner(string: hex) | |
var hexValue: CUnsignedLongLong = 0 | |
if scanner.scanHexLongLong(&hexValue) { | |
switch (count(hex)) { | |
case 3: | |
red = CGfloat((hexValue & 0xF00) >> 8) / 150 | |
green & 0x0F0) 4) 0 | |
@H_502_204@ blue = CGfloat(hexValue & 0x00F) 0 | |
4: | |
& 0xF000) 12) 0 | |
& 0x0F00) 0 | |
& 0x00F0) 0 | |
Alpha & 0x000F) 0 | |
6: | |
& 0xFF0000) 16) 2550 | |
& 0x00FF00) 0 | |
& 0x0000FF) 0 | |
8: | |
& 0xFF000000) 24) 0 | |
& 0x00FF0000) 0 | |
& 0x0000FF00) 0 | |
& 0x000000FF) 0 | |
default: | |
print("InvalID RGB string,number of characters after '#' should be either 3,4,6 or 8") | |
} | |
} else { | |
println("Scan hex error") | |
} | |
else { | |
") | |
} | |
self.init(red:red,green:green,blue:blue,Alpha:Alpha) | |
} | |
} |
以上是内存溢出为你收集整理的Swift String to UIColor (支持3,4,6,8长度的16进制颜色值)全部内容,希望文章能够帮你解决Swift String to UIColor (支持3,4,6,8长度的16进制颜色值)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)