Available categories to write a convenient to use
+ (Uicolor *) colorwithhexstring: (NSString *) color
{
NSString *cstring = [[Colorstringbytrimmingcharactersinset:[nscharactersetwhitespaceandnewlinecharacterset]] Uppercasestring];
String should be 6 or 8 characters
if ([cString length] <6) {
return [Uicolorclearcolor];
}
Strip 0X if it appears
if ([cString hasprefix:@ "0X"])
CString = [Cstringsubstringfromindex:2];
if ([CString hasprefix:@ "#"])
CString = [Cstringsubstringfromindex:1];
if ([cString length]!=6)
return [Uicolorclearcolor];
Separate into R, G, B substrings
Nsrange range;
range.location = 0;
Range.length = 2;
R
NSString *rstring = [cString substringwithrange:range];
G
Range.location = 2;
NSString *gstring = [cString substringwithrange:range];
B
Range.location = 4;
NSString *bstring = [cString substringwithrange:range];
Scan values
unsigned int r, G, B;
[[Nsscannerscannerwithstring:rstring] scanhexint:&r];
[[Nsscannerscannerwithstring:gstring] scanhexint:&g];
[[Nsscannerscannerwithstring:bstring] scanhexint:&b];
return [uicolorcolorwithred: ((float) r/255.0f) Green: ((float) g/255.0f) Blue: ((float) b/255.0f) alpha:1.0f];
}