extension UIColor { convenience init(hex: Int, alpha: CGFloat) { let r = CGFloat((hex & 0xFF0000) >> 16) / 255.0 let g = CGFloat((hex & 0x00FF00) >> 8) / 255.0 let b = CGFloat(hex & 0x0000FF) / 255.0 self.init(red: r, green: g, blue: b, alpha: alpha) } convenience init(hexString str: String, alpha: CGFloat) { let range = NSMakeRange(0, countElements(str)) let hex = (str as NSString).stringByReplacingOccurrencesOfString("[^0-9a-fA-F]", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range: range) var color: UInt32 = 0 NSScanner(string: hex).scanHexInt(&color) self.init(hex: Int(color), alpha: alpha) } }
以下のように利用します。
UIColor(hex: 0xEEEEEE, alpha: 1)