HexColorExt 是 Swift 中处理颜色的扩展。
📲 安装
HexColorExt
可在 CocoaPods 上使用
pod 'HexColorExt'
📝 如何
代码实现
import HexColorExt
生成 UIColor
带有第一个 "#" Hex 代码
let orangeColor = UIColor(hex: "#f60") // short hex
let blueColor = UIColor(hex: "#0080FF")
let blueColorWithAlpha = UIColor(hex: "#0080ff80")
不带第一个 "#" Hex 代码
let orangeColor = UIColor(hex: "f60") // short hex
let blueColor = UIColor(hex: "0080FF")
let blueColorWithAlpha = UIColor(hex: "0080ff80")
带有 Alpha 成分
let orangeColor = UIColor(hex: "f60").withAlpha(0.8)
let blueColor = UIColor(hex: "0080FF").withAlpha(0.6)
从 UIColor 生成 Hex 字符串
let blueColor = UIColor(hex: "0080FF")
let hex = blueColor.toHex() // "0080FF"
let blueColorWithAlpha = UIColor(hex: "0080ff80")
let hexWithAlpha = blueColor.toHex(alpha: true) // "0080ff80"
从 UIColor 生成 UIImage
let blueImage:UIImage = UIColor(hex: "0080FF").image()
📋 要求
- iOS 10.0+
- Xcode 11+
- Swift 5.1+