UIColor Category file in Swift (extension)


Aspect of Class Initializers.
The Swift language has two different types of initializers
They are followed by :
— designated initializers.
— convenience initializers.
Designated initializers used for initialization. Its getting the parameter to set the value in the method variable.

init(sender: String, recipient: String) {
self.sender = sender
self.recipient = recipient

timeStamp = NSDate()

Convenience initializers used for initialization. It’s getting the some parameter to set the value and set the static value for the developer needs.

convenience init(sender: String) {
self.init(sender: sender, recipient: sender)

We have some rules applied to do that initialization
Below are explained that

init(content: String, sender: String, recipient: String) {
self.content = content
//Rule 1: Calling designated Initializer from immediate superclass
super.init(sender: sender, recipient: recipient)

convenience init() {
//Rule 2: Calling another initializer in the same class
self.init(content: "")

convenience init(content: String) {
//Rule 2: Calling another initializer in same class
self.init(content: content, sender: "Myself")

convenience init(content: String, sender: String) {
//Rule 2 and 3: Calling the Designated Initializer in same class
self.init(content: content, sender: sender, recipient: sender)

Just create a swift file and paste it the below code to change
the hexadecimal string to color and set it in the view controller.

import UIKit

public extension UIColor {
   /// Return an color containing the hexdecimal string 
   /// and alpha value 
   /// hexString is variable of string
   public convenience init?(hexString: String) {
        self.init(hexString: hexString, alpha: 1.0)

   public convenience init?(hexString: String, alpha: Float) {

        var hex = hexString
        var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
        if (cString.hasPrefix("#")) {
            cString = (cString as NSString).substringFromIndex(1)
        if ((cString as NSString).length != 6) {
            hex = "808080"
        let rString = (cString as NSString).substringToIndex(2)
        var gString = (cString as NSString).substringFromIndex(2)
        gString = (gString as NSString).substringToIndex(2)
        var bString = (cString as NSString).substringFromIndex(4)
        bString = (bString as NSString).substringToIndex(2)
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        NSScanner(string: rString).scanHexInt(&r)
        NSScanner(string: gString).scanHexInt(&g)
        NSScanner(string: bString).scanHexInt(&b)
        self.init(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))


Call it in the ViewController like below code :

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        self.view.backgroundColor = UIColor.init(hexString: "#ff80ff")  // method 1
        self.view.backgroundColor = UIColor.init(hexString: "#ff80ff", alpha: 0.8)    //method 2
        // Do any additional setup after loading the view, typically from a nib.

    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s