Available categories write a convenient 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];
}
Color convert hex color in iOS to Uicolor