swift下,给控件设置颜色,有好几种方式,rbg,16进制hex数值,16进制hex字符串
我们可以统一一下,满足各种方式的设置
定义一个ColorProvider协议,针对不同类型的颜色,给定默认实现
实现之前,给大家看一下使用方式
UIColor.dynamic(0xFFFFFF, dark: 0x000000)
UIColor.dynamic(UIColor.white, dark: UIColor.black)
UIColor.dynamic(0xFFFFFF, dark: "0x000000")
0x1B1B1B.uiColor
UIColor(red: 129, green: 21, blue: 12)
"0xffffff".uiColor
"#ffffff".uiColor
0x7986E6.cgColor
利用协议来扩展Color
public protocol ColorProvider {
var uiColor: UIColor { get }
var cgColor: CGColor { get }
}
给cgColor一个默认实现
extension ColorProvider {
public var cgColor: CGColor {
return uiColor.cgColor
}
}
扩展
/// 16进制数值
public struct HexAColor {
public let hex: Int
public let alpha: CGFloat
public init(hex: Int, alpha: CGFloat = 1.0) {
self.alpha = alpha
self.hex = hex
}
}
/// 16进制字符串
public struct StrHexColor {
public let hex: String
public let alpha: CGFloat
public init(hex: String, alpha: CGFloat = 1.0) {
self.alpha = alpha
self.hex = hex
}
}
/// RGB
public struct RGBAColor {
let red: Int
let green: Int
let blue: Int
let alpha: CGFloat
public init(red: Int, green: Int, blue: Int, alpha: CGFloat = 1.0) {
self.alpha = alpha
self.blue = blue
self.green = green
self.red = red
}
}
extension默认实现
/// HEX 16进制
extension HexAColor: ColorProvider {
public var uiColor: UIColor {
let red = Double((hex & 0xFF0000) >> 16) / 255.0
let green = Double((hex & 0xFF00) >> 8 ) / 255.0
let blue = Double( hex & 0xFF ) / 255.0
return UIColor.init(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: alpha)
}
}
/// HEX 字符串
extension StrHexColor: ColorProvider {
public var uiColor: UIColor {
return hex.getColor(alpha: alpha)
}
}
/// RGB
extension RGBAColor: ColorProvider {
public var uiColor: UIColor {
UIColor(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: alpha)
}
}
UIColor
extension UIColor: ColorProvider {
public var uiColor: UIColor {
return self
}
}
Int
extension Int: ColorProvider {
public var uiColor: UIColor {
let red = Double((self & 0xFF0000) >> 16) / 255.0
let green = Double((self & 0xFF00) >> 8 ) / 255.0
let blue = Double( self & 0xFF ) / 255.0
return UIColor.init(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1)
}
}
String
extension String: ColorProvider {
public var uiColor: UIColor {
return getColor(alpha: 1)
}
func getColor(alpha: CGFloat) -> UIColor {
var cstr = trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() as NSString
if cstr.length < 6 { return .clear }
if cstr.hasPrefix("0X") {
cstr = cstr.substring(from: 2) as NSString
} else if cstr.hasPrefix("#") {
cstr = cstr.substring(from: 1) as NSString
}
if cstr.length != 6 { return .clear }
var range = NSRange.init()
range.location = 0
range.length = 2
//r
let rStr = cstr.substring(with: range);
//g
range.location = 2
let gStr = cstr.substring(with: range)
//b
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)
return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: alpha)
}
}
浅色模式和深色模式下,设置动态颜色
extension UIColor {
/// 设置动态颜色,可以直接设置Int类型的hex值,
/// - Parameter light: 浅色模式下的颜色
/// - Parameter dark: 深色模式下的颜色
public static func dynamic(_ light: ColorProvider, dark: ColorProvider, alphaComponent alpha: CGFloat? = nil) -> UIColor {
if #available(iOS 13.0, *) {
return UIColor.init { (traitCollection) -> UIColor in
if let alpha = alpha {
return traitCollection.userInterfaceStyle != .light ? dark.uiColor.withAlphaComponent(alpha) : light.uiColor.withAlphaComponent(alpha)
}
return traitCollection.userInterfaceStyle != .light ? dark.uiColor : light.uiColor
}
} else {
if let alpha = alpha {
return light.uiColor.withAlphaComponent(alpha)
}
return light.uiColor
}
}
给UIColor扩展一些常用的方法
extension UIColor {
/// rgb来颜色,不用再除255, 如 UIColor(red: 129, green: 21, blue: 12)
///
/// - Parameters:
/// - red: red component.
/// - green: green component.
/// - blue: blue component.
/// - transparency: optional transparency value (default is 1).
convenience public init?(red: Int, green: Int, blue: Int, transparency: CGFloat = 1) {
guard red >= 0 && red <= 255 else { return nil }
guard green >= 0 && green <= 255 else { return nil }
guard blue >= 0 && blue <= 255 else { return nil }
var trans = transparency
if trans < 0 { trans = 0 }
if trans > 1 { trans = 1 }
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: trans)
}
/// 随机色
public static var random: UIColor {
let red = Int.random(in: 0...255)
let green = Int.random(in: 0...255)
let blue = Int.random(in: 0...255)
return UIColor(red: red, green: green, blue: blue)!
}
}