给UIColor添加拓展方法,通过"f8f8f8"获取对应的UIColor值:
实际使用:let color = UIColor(hexColor: "f8f8f8")
importFoundation
import UIKit
extension UIColor {
convenienceinit(hexColor:String) {
varred:UInt32=0, green:UInt32=0, blue:UInt32=0
lethex = hexColorasNSString
Scanner(string: hex.substring(with:NSRange(location:0, length:2))).scanHexInt32(&red)
Scanner(string: hex.substring(with:NSRange(location:2, length:2))).scanHexInt32(&green)
Scanner(string: hex.substring(with:NSRange(location:4, length:2))).scanHexInt32(&blue)
self.init(red:CGFloat(red)/255.0, green:CGFloat(green)/255.0, blue:CGFloat(blue)/255.0, alpha:1.0)
}
}
网友评论