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)
Let’s 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 a 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 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.