Sahil...

Indian born, African raised, American passport holder. I make iPhone & iPad apps.

Get the UIColor you want!

I basically grew tired of not being able to specify the exact HEX code for a colour, instead having to resort to the defaults that UIColor offers.

So along with using Hex Color Picker, I came up with a nice little macro that you can slap into a header:

#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]

The usage is super easy, I’ll present it alongside how one would currently choose black for say, the background of a UITableView:

The old way:

[self.tableView setBackgroundColor:[UIColor black]];

my awesome new way:

[self.tableView setBackgroundColor:UIColorFromRGB(0x000000)];

And now, we all win. Yay!

  1. sahil posted this