Step 1 : Create an extension class and paste the code bellow

[java]
import Foundation
import UIKit
extension UIColor {
public convenience init?(hexString: String) {
let r, g, b, a: CGFloat

if hexString.hasPrefix("#") {
let start = hexString.index(hexString.startIndex, offsetBy: 1)
let hexColor = String(hexString[start…])

if hexColor.count == 8 {
let scanner = Scanner(string: hexColor)
var hexNumber: UInt64 = 0

if scanner.scanHexInt64(&hexNumber) {
r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
a = CGFloat(hexNumber & 0x000000ff) / 255

self.init(red: r, green: g, blue: b, alpha: a)
return
}
}
}

return nil
}
}

[/java]

Step 2 : Now use hex color code as

[java]
let whiteColor = UIColor(hexString: "#FFFFFF")
[/java]

enjoy 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *