Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Thursday, May 7, 2009

iPhone: Want to use Hex for UIColor?

Thursday, May 7, 2009   

When you need to set basic colors to things in Objective-C, it's really easy:
//This works, but come on...
cell.textColor = [UIColor grayColor];
However, if you want to be more specific normally you need to get the RGBA for the color and set it that way. That's time consuming especially if the color may be tweaked several times before the final is settled upon. In flash we can just use Hex. Now, with this little macro, you can too & it's a HUGE time saver:
#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]

- (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";
}
Shazaam!!!

Labels: ,

 
 Return to the main page
Comments:

There are currently 1 Comments:

Anonymous Anonymous said...
“Very nice. did the trick for me.”
 
 Leave a comment