Skip to content

Instantly share code, notes, and snippets.

@rtimpone
Created May 18, 2018 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtimpone/ede67e37b61b51aead796a60c89f2770 to your computer and use it in GitHub Desktop.
Save rtimpone/ede67e37b61b51aead796a60c89f2770 to your computer and use it in GitHub Desktop.
Create a UIColor from hex values
extension UIColor {
convenience init(hex: String) {
let trimmedString = hex.replacingOccurrences(of: "#", with: "")
var uint32Value: UInt32 = 0
Scanner(string: trimmedString).scanHexInt32(&uint32Value)
let intValue = Int(uint32Value)
self.init(hex: intValue)
}
convenience init(hex: Int) {
let r = CGFloat((hex >> 16) & 0xff) / 255
let g = CGFloat((hex >> 08) & 0xff) / 255
let b = CGFloat((hex >> 00) & 0xff) / 255
self.init(red: r, green: g, blue: b, alpha: 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment