Skip to content

Instantly share code, notes, and snippets.

@shadcn
Last active September 17, 2022 11:50
Show Gist options
  • Save shadcn/de147c42d7b3063ef7bc to your computer and use it in GitHub Desktop.
Save shadcn/de147c42d7b3063ef7bc to your computer and use it in GitHub Desktop.
Convert a Hex string to UIColor in Swift
// 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))
}
@vitorspencer
Copy link

Thanks guys, you just saved me quite some work! 👍

@imsoda
Copy link

imsoda commented May 18, 2016

Thanks!

@StallionV
Copy link

StallionV commented May 18, 2016

With Hexstring and Alpha separate.

extension UIColor {
    convenience init(hexString: String, alpha: Double = 1.0) {
        let hex = hexString.stringByTrimmingCharactersInSet(NSCharacterSet.alphanumericCharacterSet().invertedSet)
        var int = UInt32()
        NSScanner(string: hex).scanHexInt(&int)
        let r, g, b: UInt32
        switch hex.characters.count {
        case 3: // RGB (12-bit)
            (r, g, b) = ((int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
            (r, g, b) = (int >> 16, int >> 8 & 0xFF, int & 0xFF)
        default:
            (r, g, b) = (1, 1, 0)
        }
        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(255 * alpha) / 255)
    }
}

UIColor.init(hexString: "000000", alpha: 0.5) // r 0.0 g 0.0 b 0.0 a 0.5
UIColor.init(hexString: "#fff", alpha: 0.2) // r 1.0 g 1.0 b 1.0 a 0.2
UIColor.init(hexString: "00ff00", alpha: 1.0) // r 0.0 g 1.0 b 0.0 a 1.0
UIColor.init(hexString: "0000ff") // r 0.0 g 0.0 b 1.0 a 1.0 with alpha optional which defaults to 1

@raymond-liao
Copy link

raymond-liao commented Aug 12, 2016

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.

@arvidurs
Copy link

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)
    }
}

@cppcho
Copy link

cppcho commented Aug 17, 2016

@arvidurs Thanks 👍

@muruganandham
Copy link

thanks @arvidurs

@realAbstractionist
Copy link

@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.

@CocoaDebug
Copy link

nice work! @arvidurs

label.textColor = "#007aff".hexColor

@CaptainFarrell
Copy link

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