Skip to content

Instantly share code, notes, and snippets.

@aronbudinszky
Forked from anonymous/UIColor+Helper.swift
Last active March 22, 2018 22:18
Show Gist options
  • Save aronbudinszky/54d3a3cbd4ea34b6bae1aaeed1391ca6 to your computer and use it in GitHub Desktop.
Save aronbudinszky/54d3a3cbd4ea34b6bae1aaeed1391ca6 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 = Scanner(string: hex)
scanner.scanLocation = 1 // skip #
var rgb: UInt32 = 0
scanner.scanHexInt32(&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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment