Skip to content

Instantly share code, notes, and snippets.

@Ariandr
Last active March 5, 2018 11:43
Show Gist options
  • Select an option

  • Save Ariandr/beff2118d4ff60a43d7196146cc3a105 to your computer and use it in GitHub Desktop.

Select an option

Save Ariandr/beff2118d4ff60a43d7196146cc3a105 to your computer and use it in GitHub Desktop.
Creating UIColor from a hex string. Example: let color = UIColor(hex: "FFFFFF")
import UIKit
extension UIColor {
convenience init(hex: String) {
let scanner = Scanner(string: hex)
scanner.scanLocation = 0
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
let r = (rgbValue & 0xff0000) >> 16
let g = (rgbValue & 0xff00) >> 8
let b = rgbValue & 0xff
self.init(
red: CGFloat(r) / 0xff,
green: CGFloat(g) / 0xff,
blue: CGFloat(b) / 0xff, alpha: 1
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment