Handling Colors Better in Your Apps

When I’m working on an app something that I try to avoid is to have lines of code like this mixed in my display code:

view.backgroundColor = [UIColor colorWithRed:0.75f green:0.64f blue:0.64f];

Instead, I create a category on UIColor — called something like UIColor(MyAppColors) — and add a class method whenever I need to use a new color to the app. That makes the line above something more like:

view.backgroundColor = [UIColor alb_defaultBackgroundColor];

The main reason I do this is that if I ever want to tweak a color which gets reused in multiple places, I only need to change the category method for it to be updated everywhere. It’s a good practice to avoid having literal strings and numbers strewn throughout your code, and this is a good example of why.

Collin Donnell @collin