Handle custom UIColors in iOS

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.

 

new color set

 

  • Give proper naming convention to your Color Set:

 

naming convention to color set

 

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

 

attribute inspector
attribute inspector

 

 

  • Add all required custom colors by creating color sets.

 

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.

 

UI view

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. 

 

About RemotePanda

RemotePanda is a personalized platform for companies to hire remote talent and get the quality work delivered from the city Pune. The resources in our talent pool are our close network connections. While connecting them with you, we make sure to manage the quality, growth, legalities, and the delivery of their work. The idea is to make remote work successful for you. Get in touch with us to learn why RemotePanda is the best fit solution for your business requirements.

 

 

Comments are closed.