Move Custom Colours Into a Category on UIColor
Published September 27, 2013
This article was published a really long time ago.
Its value then was likely questionable. Its value now is certainly pure nostalgia.
I've been working on a project with a large amount of custom colors defined programatically for the UI, meaning a large amount of my View Controller code started to look like this...
thing.backgroundColor = [UIColor colorWithRed:39.0/255.0
green:174.0/255.0
blue:96.0/255.0
alpha:1.0];
Repeat it n times and your methods are getting bloated with RGBa like above.
To clean things up a bit, I create a Category on UIColor and defined all my custom colors in methods on that category.
@interface UIColor (ProjectColors)
+ (UIColor *)projectGreen;
+ (UIColor *)projectBlue;
+ (UIColor *)twitterBlue;
+ (UIColor *)facebookBlue;
@end
@implementation UIColor (ProjectColors)
+ (UIColor *)projectGreen
{
return [UIColor colorWithRed:39.0f/255.0f
green:174.0f/255.0f
blue:96.0f/255.0f
alpha:1.0f];
}
@end
Now my View Controller isn't full of RGBa, and those custom colors now have nice names I can refer to later.
When I want the green I use for this project, I get it like this:
thing.backgroundColor = [UIColor projectGreen];