Last active
September 17, 2022 11:50
-
-
Save shadcn/de147c42d7b3063ef7bc to your computer and use it in GitHub Desktop.
Convert a Hex string to UIColor in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Creates a UIColor from a Hex string. | |
func colorWithHexString (hex:String) -> UIColor { | |
var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString | |
if (cString.hasPrefix("#")) { | |
cString = cString.substringFromIndex(1) | |
} | |
if (countElements(cString) != 6) { | |
return UIColor.grayColor() | |
} | |
var rString = cString.substringToIndex(2) | |
var gString = cString.substringFromIndex(2).substringToIndex(2) | |
var bString = cString.substringFromIndex(4).substringToIndex(2) | |
var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0; | |
NSScanner.scannerWithString(rString).scanHexInt(&r) | |
NSScanner.scannerWithString(gString).scanHexInt(&g) | |
NSScanner.scannerWithString(bString).scanHexInt(&b) | |
return UIColor(red: Float(r) / 255.0, green: Float(g) / 255.0, blue: Float(b) / 255.0, alpha: Float(1)) | |
} |
For Swift 3.0
extension String {
var hexColor: UIColor {
let hex = trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
var int = UInt32()
Scanner(string: hex).scanHexInt32(&int)
let a, r, g, b: UInt32
switch hex.characters.count {
case 3: // RGB (12-bit)
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
case 6: // RGB (24-bit)
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8: // ARGB (32-bit)
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
return .clear
}
return UIColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
}
}
@arvidurs Thanks 👍
thanks @arvidurs
@arvidurs Really helped!
Just wanna add that you have to put this code outside of any class. Otherwise you are probably going to get an exception.
nice work! @arvidurs
label.textColor = "#007aff".hexColor
in swift 4.0
convenience init(hex: String)
{
let characterSet = NSCharacterSet.whitespacesAndNewlines as! NSMutableCharacterSet
characterSet.formUnion(with: NSCharacterSet.init(charactersIn: "#") as CharacterSet)
let cString = hex.trimmingCharacters(in: characterSet as CharacterSet).uppercased()
if (cString.count != 6) {
self.init(white: 1.0, alpha: 1.0)
} else {
var rgbValue: UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
self.init(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0))
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've made another UIColor extension in Swift 2.2, wish to help some one:
extension UIColor {
convenience init(hex: Int, alpha: Double = 1.0) {
self.init(red: CGFloat((hex>>16)&0xFF)/255.0, green: CGFloat((hex>>8)&0xFF)/255.0, blue: CGFloat((hex)&0xFF)/255.0, alpha: CGFloat(255 * alpha) / 255)
}
}
Sample to use:
UIColor(hex: 0xffffff) // r 1.0 g 1.0 b 1.0 a 1.0
UIColor(hex: 0xffffff, alpha: 0.5) // r 1.0 g 1.0 b 1.0 a 0.5
p.s.: Sorry for the abnormal code format, the 'Insert code' function is not good for me.