给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)
}
}
本文介绍如何为UIColor类添加扩展方法,以便通过十六进制颜色代码(如f8f8f8)创建UIColor实例。该方法首先解析十六进制字符串为红、绿、蓝三个颜色分量,并将其转换为UIColor对象。
735

被折叠的 条评论
为什么被折叠?



