HexColorExt is an extension for handling color in Swift.
HexColorExt
is available on CocoaPods:
pod 'HexColorExt'
import HexColorExt
Wiith first "#" Hex Codes
let orangeColor = UIColor(hex: "#f60") // short hex
let blueColor = UIColor(hex: "#0080FF")
let blueColorWithAlpha = UIColor(hex: "#0080ff80")
Without first "#" Hex Codes
let orangeColor = UIColor(hex: "f60") // short hex
let blueColor = UIColor(hex: "0080FF")
let blueColorWithAlpha = UIColor(hex: "0080ff80")
With Alpha Component
let orangeColor = UIColor(hex: "f60").withAlpha(0.8)
let blueColor = UIColor(hex: "0080FF").withAlpha(0.6)
let blueColor = UIColor(hex: "0080FF")
let hex = blueColor.toHex() // "0080FF"
let blueColorWithAlpha = UIColor(hex: "0080ff80")
let hexWithAlpha = blueColor.toHex(alpha: true) // "0080ff80"
let blueImage:UIImage = UIColor(hex: "0080FF").image()
- iOS 10.0
- Xcode 11
- Swift 5.1