Skip to content

Instantly share code, notes, and snippets.

@rxmichael
Created September 28, 2016 14:11
Show Gist options
  • Save rxmichael/d43a029ad901728f7dcf61e8029a412e to your computer and use it in GitHub Desktop.
Save rxmichael/d43a029ad901728f7dcf61e8029a412e to your computer and use it in GitHub Desktop.
Extension for creating UIColor with hex values
import Foundation
import UIKit
/** Extends UIColor to initialize UIColor using hex values, f.e. UIColor(hex: 0x8046A2) */
extension UIColor {
convenience init(hex: Int, alpha: CGFloat) {
let r = CGFloat((hex & 0xFF0000) >> 16)/255
let g = CGFloat((hex & 0xFF00) >> 8)/255
let b = CGFloat(hex & 0xFF)/255
self.init(red: r, green: g, blue: b, alpha: alpha)
}
convenience init(hex: Int) {
self.init(hex:hex, alpha:1.0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment