Creating UIColor objects from hex values

It’s pretty inconvenient to create UIColor objects while developing apps for the iPhone, as you need to specific separate values for the RGB parts: red, green, blue.
I found a trick online a few weeks ago on how to automatically generate that code, by simply using a mac
#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 looks something like this:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (nil == cell) {

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"] autorelease];

}

cell.textColor = UIColorFromRGB(0x333333);

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

cell.text = @"Testing 1 2 3";

}
This entry was posted in iOS by Muzammil. Bookmark the permalink.

About Muzammil

Hey Guys, I am Muzammil Mohammad. Having 7 years of experience in iOS development. I am here to share my knowledge with all mobile application developers and those companies who are stuck on development (due to bad quality of code or don't know what to do) or want to know how to build a good mobile app. I believe I am more expert in UI and Architecture design of mobile app. So if you need my helps or assistant for your mobile app projects, Feel free to contact me. I will try my best to improve the quality of your app. Or you can hire me to develop your apps. I’d love to talk with you. Best!

4 thoughts on “Creating UIColor objects from hex values

  1. Hi Muzammil,

    This is Rahul from New Delhi. I have got your blog address from your Orkut account. I have an iPhone project requirement which I would like to outsource to you. Please let me know if you are interested in picking up this project and I will send you the complete details.

    If interested then please get back to me at er.rahulkamboj@yahoo.com ASAP with your contact number as it is an urgent requirement.

    Regards,
    Rahul Kamboj

      • I would like to express tkahns to you for rescuing me from this type of trouble. Right after researching throughout the internet and obtaining suggestions that were not pleasant, I assumed my life was well over. Being alive without the answers to the difficulties you have fixed by means of the short post is a serious case, as well as the ones which might have negatively affected my entire career if I had not noticed your web blog. Your own personal competence and kindness in taking care of all the details was tremendous. I’m not sure what I would have done if I had not discovered such a solution like this. I am able to now relish my future. Thanks very much for this reliable and results-oriented help. I won’t hesitate to suggest your web site to any person who will need tips on this situation.

Leave a Reply to Mia Cancel reply

Your email address will not be published. Required fields are marked *