使用 RGBA 十六进制字符串创建自动释放颜色的便捷方法。
// Solid color
let strokeColor = UIColor("#FFCC00").cgColor
// Color with alpha
let fillColor = UIColor("#FFCC00DD").cgColor
// Supports shorthand 3 character representation
let backgroundColor = UIColor("#FFF")
// Supports shorthand 4 character representation (with alpha)
let menuTextColor = UIColor("#013E")
// "#FF0000FF"
let hexString = UIColor.red.hexString()
// Convert shorthand 4 character representation (with alpha) from argb to rgba
if let rgba = "#AFFF".argb2rgba {
let androidBackgroundColor = UIColor(rgba)
}
// Convert 8 character representation (with alpha) from argb to rgba
if let rgba = "#AAFFFFFF".argb2rgba {
let androidFrontColor = UIColor(rgba)
}
发布说明
- 升级到 Swift 5。
- macOS 开始支持。
安装
Swift Package Manager
要将包依赖添加到您的Xcode项目中,选择“文件 > Swift包 > 添加包依赖”,并在文本框中输入https://github.com/yeahdongcn/UIColor-Hex-Swift。
CocoaPods
只需在您的Podfile
中添加以下行
# required by CocoaPods 0.36.0.rc.1 for Swift Pods
use_frameworks!
pod 'UIColor_Hex_Swift', '~> 5.1.9'
然后在哪里使用它导入它
import UIColor_Hex_Swift
(需要CocoaPods v0.36或更高版本。有关详细信息,请参阅此博客文章)
Carthage
只需在您的Cartfile
中添加以下行
github "yeahdongcn/UIColor-Hex-Swift" >= 5.1.9
然后将HexColor.framework添加到Xcode项目中的框架列表中
然后在哪里使用它导入它
import HEXColor
更多内容请参阅RSBarcodes_Swift和objc版本