Created
September 28, 2016 14:11
-
-
Save rxmichael/d43a029ad901728f7dcf61e8029a412e to your computer and use it in GitHub Desktop.
Extension for creating UIColor with hex values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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