- #pragma mark-color convert hex color in iOS to Uicolor
- + (Uicolor *) colorwithhexstring: (NSString *) color
- {
- 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 ([cString hasprefix:@"0X"])
- cString = [cString substringfromindex:2];
- 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:1.0f];
- }
hexadecimal colors in iOS are converted to Uicolor