UIColor+Hex from string fix

This commit is contained in:
Ivan Smolin 2016-09-09 12:28:05 +03:00
parent ea4eb6db0e
commit 8ed1876c7e
1 changed files with 19 additions and 15 deletions

View File

@ -87,28 +87,32 @@ public extension UIColor {
public convenience init?(hexString: String, alpha: CGFloat = 1) {
let hexStr = hexString.hasPrefix("#") ? hexString.substringFromIndex(hexString.startIndex.advancedBy(1)) : hexString
switch hexStr.characters.count {
case 3:
let charactersCount = hexStr.characters.count
switch charactersCount {
case 3, 4:
if let hex = UInt16(hexStr, radix: 16) {
self.init(hex3: hex, alpha: alpha)
if charactersCount == 3 {
self.init(hex3: hex, alpha: alpha)
} else {
self.init(hex4: hex)
}
} else {
return nil
}
case 4:
if let hex = UInt16(hexStr, radix: 16) {
self.init(hex4: hex)
}
case 6:
case 6, 8:
if let hex = UInt32(hexStr, radix: 16) {
self.init(hex6: hex, alpha: alpha)
}
case 8:
if let hex = UInt32(hexStr, radix: 16) {
self.init(hex8: hex)
if charactersCount == 6 {
self.init(hex6: hex, alpha: alpha)
} else {
self.init(hex8: hex)
}
} else {
return nil
}
default:
return nil
}
return nil
}
}