设置字体和颜色
lab.textColor = UIColor.init(hexColor: "795928") lab.font = UIFont.systemFont(ofSize: 32, weight: UIFont.Weight.black)
设置html 导图颜色
extension UIColor { /// 用十六进制颜色创建UIColor /// /// - Parameter hexColor: 十六进制颜色 (0F0F0F) convenience init(hexColor: String) { // 存储转换后的数值 var red:UInt32 = 0, green:UInt32 = 0, blue:UInt32 = 0 // 分别转换进行转换 Scanner(string: hexColor[0..<2]).scanHexInt32(&red) Scanner(string: hexColor[2..<4]).scanHexInt32(&green) Scanner(string: hexColor[4..<6]).scanHexInt32(&blue) self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1.0) } convenience init(hexColor: String, alpha: CGFloat) { // 存储转换后的数值 var red:UInt32 = 0, green:UInt32 = 0, blue:UInt32 = 0 // 分别转换进行转换 Scanner(string: hexColor[0..<2]).scanHexInt32(&red) Scanner(string: hexColor[2..<4]).scanHexInt32(&green) Scanner(string: hexColor[4..<6]).scanHexInt32(&blue) self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: alpha) } }
/// 初始化:十六进制颜色 /// /// - Parameter hexString: 十六进制颜色字符串(1:有#,2:没有#,3:含有0X) convenience init(hexString: String, alpha: CGFloat = 1.0) { var cstr = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() as NSString if(cstr.length < 6){ self.init(red: 0, green: 0, blue: 0, alpha: 0) return } if(cstr.hasPrefix("0X")){ cstr = cstr.substring(from: 2) as NSString } if(cstr.hasPrefix("#")){ cstr = cstr.substring(from: 1) as NSString } if(cstr.length != 6){ self.init(red: 0, green: 0, blue: 0, alpha: 0) return } var range = NSRange.init() range.location = 0 range.length = 2 let rStr = cstr.substring(with: range) range.location = 2 let gStr = cstr.substring(with: range) range.location = 4 let bStr = cstr.substring(with: range) var r :UInt32 = 0x0 var g :UInt32 = 0x0 var b :UInt32 = 0x0 Scanner.init(string: rStr).scanHexInt32(&r) Scanner.init(string: gStr).scanHexInt32(&g) Scanner.init(string: bStr).scanHexInt32(&b) self.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: alpha) }