The project often uses the color conversion, some through the hexadecimal to the number to the color, wants the simple point to pass through the string directly, simply extends the classification Uicolor, the code is as follows:
+ (Uicolor *) Colorwithhex: (NSString *) hexcolor{return [self Colorwithhex:hexcolor alpha:1.0f];} Http://www.cnblogs.com/xiaofeixiang iOS technology: 228407086+ (Uicolor *) Colorwithhex: (NSString *) Hexcolor Alpha: (float) alpha{//Remove space NSString *colorstr = [[Hexcolor stringbytrimmingcharactersinset:[nscharacterset WhitespaceAndNewlineCh Aracterset]] uppercasestring]; String should be 6 or 8 characters if ([Colorstr length] < 6| | [Colorstr length]>7) {return [Uicolor Clearcolor]; }//if ([Colorstr hasprefix:@ "#"]) {colorstr = [colorstr substringfromindex:1]; } Nsrange range; range.location = 0; Range.length = 2; Red NSString *redstring = [Colorstr substringwithrange:range]; Green range.location = 2; NSString *greenstring = [Colorstr substringwithrange:range]; Blue range.location = 4; NSString *bluestring= [Colorstr Substringwithrange:range]; Scan values unsigned int red, green, blue; [[NSScanner Scannerwithstring:redstring] scanhexint:&red]; [[Nsscanner scannerwithstring:greenstring] scanhexint:&green]; [[Nsscanner scannerwithstring:bluestring] scanhexint:&blue]; return [Uicolor colorwithred: ((float) red/255.0f) Green: ((float) green/255.0f) Blue: ((float) blue/255.0f) Alpha:alpha] ;}
iOS Development-16 binary color conversion