TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Dec 2017 |
SwiftSwift Version | 4.0 |
SPMSupports SPM | ✗ |
Maintained by Jason Nam.
Convert Hex String into UIColor
Just copy the file ‘UIColor+HexString.swift’ into your project.
// If hex string is not valid, init with white color.
view.backgroundColor = UIColor(hexString: "#123456")
// This class method verifies hex string and return nil if it is not valid.
view.backgroundColor = UIColor.colorWithHexString("#123456")
NSLog("\(UIColor.red.hexString ?? "ERROR")")
The Result will be in the form of #RRGGBB or #AARRGGBB
There is also an extension for String class.
view.backgroundColor = "#123456".toColor()
FYI, above convenience init method and class method are checking if the string is hex color string, so this is not necessary if you use them.
if let hexString = UIColor.validateHexString("#123456") {
// Do what you want
}
nil if the string is not a color hex string. Return the string when it is.
Any feedback and pull requests are welcome :)
HexString is available under the MIT license. See the LICENSE file for more info.