handle custom uicolors in ios

Handle custom UIColors in iOS

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 the color to some UIView than we’ll do it on storyboard via attributed inspector or we’ll use one of the following approaches with UIColor class programmatically:

  • 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 in one place. You can easily add your custom color as a color set. An interesting fact is you can give the proper naming convention to each color. Also, it will help 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?

Let’s see step by step how we can implement this :

You need to add “New Color set” in your assets file.

You need to add “New Color set” in your assets file.

Give proper naming convention to your Color Set:

Give proper naming convention to your Color Set

Select a color set and assign required color through attributed inspector.

Give proper naming convention to your Color Set
Give proper naming convention to your Color Set

Add all required custom colors by creating color sets.

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 a storyboard.

Conclusion:

 

Alright, guys, those are some tips for using the colors properly in iOS. Hopefully next time I’ll come up with some other feature. 

No Comments

Post a Comment

Comment
Name
Email
Website