iosswiftuicolorswift-extensions

Extension for UIColor with custom colors it is real?


I have some custom colors for my application and now its saves like a dictionary, but I think this is no really good idea and I want to do extension for UIColor with a custom color.

That may look like this

var newColor = UIColor.MyColor // like UIColor.white

Maybe I should add to extension an enumeration with my colors?


Solution

  • Create class property in UIColor extension

    extension UIColor
    {
      class var themeColor:UIColor {
        return UIColor(red: 210.0/255.0, green: 105.0/255.0, blue: 130.0/255.0, alpha: 1.0)
      }
    }
    

    OR

    extension UIColor {
      static let themeColor = UIColor(red: 210.0/255.0, green: 105.0/255.0, blue: 130.0/255.0, alpha: 1.0)
    }
    

    Usage

    self.view.backgroundColor = UIColor.themeColor