Customizing UIColor


The default list of color settings in UIColor is fairly limited: black, darkGray, lightGray, white, gray, red, green, blue, cyan, yellow, magenta, orange, purple, brown, and clear are the choices.

Other than limited choice, a problem with the default palette is that there's no control over how each specific color is defined (e.g., the green might be too light or too dark for a given application, etc.).

Following this suggestion (thanks ooo27), here's how to set UIColor to any html hex color code.

Create a macro within the class implementation (.m) file:

#define HEXCOLOR(c) [UIColor colorWithRed:((c>>24)&0xFF)/255.0 \
                                    green:((c>>16)&0xFF)/255.0 \
                                     blue:((c>>8)&0xFF)/255.0 \
                                    alpha:((c)&0xFF)/255.0];

And then use it when initializing any UIColor class.

For example, try:

UIColor* c = HEXCOLOR(0x006600ff);

instead of:

UIColor* c = [UIColor greenColor];