Skip to content

Instantly share code, notes, and snippets.

Created September 13, 2015 14:04
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/fd07ecf47591c9f9ed1a to your computer and use it in GitHub Desktop.
Save anonymous/fd07ecf47591c9f9ed1a to your computer and use it in GitHub Desktop.
UIColor from Hex String in Swift
import UIKit
extension UIColor {
convenience init(hex: String, alpha: CGFloat = 1) {
assert(hex[hex.startIndex] == "#", "Expected hex string of format #RRGGBB")
let scanner = NSScanner(string: hex)
scanner.scanLocation = 1 // skip #
var rgb: UInt32 = 0
scanner.scanHexInt(&rgb)
self.init(
red: CGFloat((rgb & 0xFF0000) >> 16)/255.0,
green: CGFloat((rgb & 0xFF00) >> 8)/255.0,
blue: CGFloat((rgb & 0xFF) )/255.0,
alpha: alpha)
}
}
@matoelorriaga
Copy link

thanks!

@a-mehta
Copy link

a-mehta commented Feb 2, 2016

Awesome!

@mstukalo
Copy link

thank you!

@Nicholas-Swift
Copy link

Thanks, this is awesome!

@legras
Copy link

legras commented Oct 4, 2016

Nice!

@bryanstrader
Copy link

@zuyao1988
Copy link

Thx! and nice

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