HexString 1.0.0

HexString 1.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Dec 2017
SwiftSwift Version 4.0
SPMSupports SPM

Maintained by Jason Nam.




HexString 1.0.0

HexString

Convert Hex String into UIColor

Installation

Manual

Just copy the file ‘UIColor+HexString.swift’ into your project.

Usage

Get UIColor from Hex String

// 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")

Get Hex String from UIColor

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

Validate

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.

Available formats

  • #RGB
  • #ARGB
  • #RRGGBB
  • #AARRGGBB

Contact

Any feedback and pull requests are welcome :)

Jason Nam
Website
Email

License

HexString is available under the MIT license. See the LICENSE file for more info.