I'm an Irish iPhone developer, currently migrating my skills from the web to Apples most popular mobile computing device. Based in County Leitrim, Ireland and working from home as a freelance developer in PHP, Perl, and Objective-C.

Contact me at andy@luibh.ie.

Creating UIColor Objects from Hex Values

If you come to the iPhone from a web background, or are simply more used to hex based colors it can be a bit of pain working out their RGB equivalent for use as a UIColor.

Fortunately though there is a rather simple work around in the form of a macro.

#define UIColorFromRGB(rgbValue) [UIColor \
    colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
    green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
    blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

Usage is also delightfully simple, and is implemented like this:

cell.textColor = UIColorFromRGB(0x333333);

Where the 333333 is your normal hex color value minus the #.