tags:

views:

73

answers:

2

How can i set CIColor from HTML color format? For example i read from file "#e0e0e0" and i need set this color to UILable. when i try use [UIColor colorWithRed: green: blue: alpha:1.0] i found that it's use float number that define color in %. How can i use LONG color format that set backgroundColor to UILable?

+2  A: 

Each color is a hex number with a maximum of 0xff (255 decimal). Divide the current value by 255 and you'll have your float to create a CIColor.

Chuck
+2  A: 

Taken from this guide: http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars

// Separate into r, g, b substrings  
NSRange range;  
range.location = 0;  
range.length = 2;  
NSString *rString = [cString substringWithRange:range];  

range.location = 2;  
NSString *gString = [cString substringWithRange:range];  

range.location = 4;  
NSString *bString = [cString substringWithRange:range];  

// Scan values  
unsigned int r, g, b;  
[[NSScanner scannerWithString:rString] scanHexInt:&r];  
[[NSScanner scannerWithString:gString] scanHexInt:&g];  
[[NSScanner scannerWithString:bString] scanHexInt:&b];  

return [UIColor colorWithRed:((float) r / 255.0f)  
                       green:((float) g / 255.0f)  
                        blue:((float) b / 255.0f)  
                       alpha:1.0f];
Mobs
If you don't want the overhead of NSScanner, look at strtol().
nall