+ (Uicolor *) colorwithhexstring: (NSString *) color alpha: (cgfloat) Alpha
{
Delete spaces in a string
NSString *cstring = [[Color Stringbytrimmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset]] Uppercasestring];
String should be 6 or 8 characters
if ([cString length] < 6)
{
return [Uicolor Clearcolor];
}
Strip 0X if it appears
If it starts with 0x, then the string is truncated, and the string starts at the position indexed to 2, up to the end
if ([cString hasprefix:@ "0X"])
{
cString = [cString substringfromindex:2];
}
If it starts with #, then the string is truncated, and the string starts at the position indexed to 1, up to the end
if ([CString hasprefix:@ "#"])
{
cString = [cString substringfromindex:1];
}
if ([cString length]! = 6)
{
return [Uicolor Clearcolor];
}
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;
[[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:alpha];
}
The default alpha value is 1
+ (Uicolor *) colorwithhexstring: (NSString *) color
{
return [self colorwithhexstring:color alpha:1.0f];
}
iOS recognition 16 binary color Table method