-(void) viewdidload {
[superviewdidload];
Additional setup after loading the view, typically from a nib.
uicolor *color = [selfcolorwithhexstring: /c18>@ "#81c52b"];
[self. View setbackgroundcolor: color];
}
-(uicolor *) colorwithhexstring: (nsstring *) Stringtoconvert
{
nsstring *cstring = [[Stringtoconvertstringbytrimmingcharactersinset: [ Nscharacterset whitespaceandnewlinecharacterset]]uppercasestring]; // string Processing
// example,stringtoconvert #ffffff
if ([cStringlength] < 6)
return [uicolorredcolor]; // If non-hexadecimal, returns white
if ([cStringhasprefix:@ "#"])
cString = [cString substringfromindex:1]; // turn around.
if ([cStringlength]! = 6)// head non-hexadecimal, Back to White
return [uicolorgreencolor];
// RGB values are taken separately
nsrange range;
range. Location = 0 ;
range. length = 2 ;
nsstring *rstring = [cStringsubstringwithrange: Range];
range. Location = 2 ;
nsstring *gstring = [cStringsubstringwithrange: Range];
range. Location = 4 ;
nsstring *bstring = [cStringsubstringwithrange: Range];
unsignedint R, G, B;
//nsscanner converts a scanned string into an Int type
[[Nsscanner scannerwithstring:rstring] scanhexint:&r];
[[Nsscanner scannerwithstring:gstring] scanhexint:&g];
[[Nsscanner scannerwithstring:bstring] scanhexint:&b];
// convert to uicolor
return [Uicolor colorwithred: ((float) R/255.0f c18>)
Green: ((float) G/ 255.0f )
Blue: ((float) b/ 255.0f )
Alpha: 1.0f ];
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[IOS] convert 16 binary colors to Uicolor