Skip to content

Instantly share code, notes, and snippets.

@noa4021J
Created May 3, 2019 08:08
Show Gist options
  • Save noa4021J/acb0257d0f2f85313540f61b2690842c to your computer and use it in GitHub Desktop.
Save noa4021J/acb0257d0f2f85313540f61b2690842c to your computer and use it in GitHub Desktop.
//HEX値でUIColorを指定できるようにする
extension UIColor {
convenience init(hexcode: String, alpha: CGFloat) {
let string = hexcode.replacingOccurrences(of: "#", with: "")
let char = string.map{ String($0) }
let r = CGFloat(Int(char[0]+char[1], radix:16) ?? 0) / 255.0
let g = CGFloat(Int(char[2]+char[3], radix:16) ?? 0) / 255.0
let b = CGFloat(Int(char[4]+char[5], radix:16) ?? 0) / 255.0
self.init(red: r, green: g, blue: b, alpha: min(max(alpha,0),1))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment