I am trying to use hex color values in Swift, instead of the few standard ones that UIColor
allows you to use, but I have no idea how to do it.
Example: how would I use #ffffff
as a color?
I am trying to use hex color values in Swift, instead of the few standard ones that UIColor
allows you to use, but I have no idea how to do it.
Example: how would I use #ffffff
as a color?
Swift 3: Hex and CSS color name support via a UIColor
Gist code
Example strings:
Orange
,Lime
,Tomato
, etc.Clear
,Transparent
,nil
, and empty string yield[UIColor clearColor]
abc
abc7
#abc7
00FFFF
#00FFFF
00FFFF77
Playground output:
Here's a Swift extension on
UIColor
that takes a hex string:Swift 2.0:
In viewDidLoad()
Swift 4.0
use this Single line of method
You have to create new Class or use any controller where u need to use Hex color. This extension class provide you UIColor that will convert Hex to RGB color.
Swift 4 : Combining the answers of Sulthan and Luca Torella :
Usage examples:
This answer shows how to do it in Obj-C. The bridge is to use