Handle custom UIColors with correct way in iOS

Kishan Nakum
3 min readFeb 9, 2018

--

If you’re an App developer then you definitely faced some challenges while dealing with multiple color codes that you’ve to use in your more than one apps .

If we want to set color to some UIView than we’ll do it on storyboard via attributed inspector or we’ll use one of the following approach with UIColor class programatically :

  • init(white: CGFloat, alpha: CGFloat)
  • init(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat)
  • init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
  • init(CGColor cgColor: CGColor!)
  • init(patternImage image: UIImage)
  • init(CIColor ciColor: CIColor)

This seems to be boring while we have to set it by giving proper values, for example :

self.view.backgroundColor = UIColor(red: 120/255, green: 150/255, blue: 200/255, alpha: 1)

Apple has introduced something called Color Sets from Xcode 9 . Color Sets helps you to define all your colors at one place. You can easily add your custom color as color set. Interesting fact is you can give proper naming convention to each colors . Also it will helpful while you want to change the color values, you will change at one place and it will reflect everywhere. As a developer you can easily remember that color by its name also you can take reference and use it in another project. This makes our life easy right ?

Lets see step by step how we can implement this :

  • You need to add “New Color set” in your assets file.
  • Give proper naming convention to your Color Set:
  • Select color set and assign required color through attributed inspector .
  • Add all required custom colors by creating color sets.
  • Now it’s time to use this Custom colors into our app. First we’ll see how we can set colors to UIView via storyboard.
  • You can use custom color by programatically approach as well. Check this…..

Alright guys , those are some tips for using the colors properly in iOS. Hopefully next time I’ll come up with some other feature . Meanwhile if you’re interested for Reactive programming in iOS , you would probably love to check my article on this : Reactive Programming in Swift Part 1

Feel free to add me on Linkedin , Happy coding……..

--

--