在项目开发中,控件颜色的设置是必须的,但很多时候我们获取到的都是16进制的色彩值,为了方便使用,对UIColor做了一些简单的扩展,以便于直接使用16进制设置
extension UIColor {
///随机色
class var random:UIColor {
return UIColor(red: CGFloat(arc4random_uniform(256))/255.0, green: CGFloat(arc4random_uniform(256))/255.0, blue: CGFloat(arc4random_uniform(256))/255.0, alpha: 1)
}
/// 通过哈希值返回颜色
///
/// - Parameter hexColor: 哈希值
/// - Returns: <#return value description#>
class func hexColor(_ hexColor:Int) ->UIColor {
let red = CGFloat((hexColor & 0xFF0000) >> 16)/255.0
let green = CGFloat((hexColor & 0xFF00) >> 8)/255.0
let blue = CGFloat(hexColor & 0xFF)/255.0
return UIColor(red: red, green: green, blue: blue, alpha: 1)
}
/// 通过哈希值字符返回颜色
///
/// - Parameter hexColor: 哈希值字符
/// - Parameter alpha: 透明度
/// - Returns: <#return value description#>
class func hexColor(_ hexColor:String ,alpha:CGFloat = 1) ->UIColor {
var cString = hexColor.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
// 去掉前缀
if cString.hasPrefix("0X") {
let start = cString.index(cString.startIndex, offsetBy: 2)
cString = String(cString[start..<cString.endIndex])
}
if cString.hasPrefix("#") {
let start = cString.index(cString.startIndex, offsetBy: 1)
cString = String(cString[start..<cString.endIndex])
}
guard cString.count == 6 else {
return .black
}
var redStr:String = ""
var greenStr:String = ""
var blueStr:String = ""
for i in 0...2 {
let start = cString.index(cString.startIndex, offsetBy: i * 2)
let end = cString.index(start, offsetBy: 2)
if i == 0 {
redStr = String(cString[start..<end])
}else if i == 1 {
greenStr = String(cString[start..<end])
}else{
blueStr = String(cString[start..<end])
}
}
var r:UInt64 = 0
Scanner(string: redStr).scanHexInt64(&r)
var g:UInt64 = 0
Scanner(string: greenStr).scanHexInt64(&g)
var b:UInt64 = 0
Scanner(string: blueStr).scanHexInt64(&b)
return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: alpha)
}
}
还是比较简单😁
来源:oschina
链接:https://my.oschina.net/wshzj/blog/4314228