/** * 16 binary converted to Uicolor * * @param stringtoconvert Hex String * * @return Uicolor*/+ (Uicolor *) Hexstringtocolor: (NSString *) stringtoconvert{NSString*cstring =[[Stringtoconvert stringbytrimmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset]] Uppercasestring]; //String should be 6 or 8 characters if([cString Length] <6)return[Uicolor Blackcolor]; //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 Blackcolor]; //separate into R, G, b substringsNsrange Range; Range.location=0; Range.length=2; NSString*rstring =[cString Substringwithrange:range]; Range.location=2; NSString*gstring =[cString Substringwithrange:range]; Range.location=4; NSString*bstring =[cString Substringwithrange:range]; //Scan ValuesUnsignedintR, 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];}
IOS hex string converted to Uicolor