Skip to content

Instantly share code, notes, and snippets.

@lbrndnr
Created April 29, 2015 09:39
Show Gist options
  • Save lbrndnr/705946cb937fadeebca4 to your computer and use it in GitHub Desktop.
Save lbrndnr/705946cb937fadeebca4 to your computer and use it in GitHub Desktop.
UIColor to init them using hex values: UIColor(0x8046A2)
extension UIColor {
convenience init(_ hexValue: Int) {
let r = (hexValue & 0xFF0000) >> 16
let g = (hexValue & 0x00FF00) >> 8
let b = hexValue & 0x0000FF
self.init(red: CGFloat(r)/255, green: CGFloat(g)/255, blue: CGFloat(b)/255, alpha: 1)
}
}
@a2
Copy link

a2 commented Apr 29, 2015

I would add @objc(xxx_initWithHexValue:). Because it's a category on UIColor, it pollutes the "public" namespace.

@lbrndnr
Copy link
Author

lbrndnr commented May 7, 2015

@a2 I wasn't using this with objc really but that would make sense otherwise :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment