16进制字符串转换成UIColor,返回UIColor
1.建立一个UIColor的category,命名为UIColor+Helper
然后在UIColor+Helper.h中:
//16进制字符串转换成UIColor,返回UIColor
+ (UIColor *)colorWithHexStr:(NSString *)colorStr;
在UIColor+Helper.m中:
//16进制字符串转换成UIColor
+ (UIColor *)colorWithHexStr:(NSString *)colorStr{
NSString * redStr = [colorStr substringWithRange:NSMakeRange(0, 2)];
NSScanner * redScanner = [NSScanner scannerWithString:redStr];
unsigned int redIntValue;
[redScanner scanHexInt:&redIntValue];
NSString * greenStr = [colorStr substringWithRange:NSMakeRange(2, 2)];
NSScanner * greenScanner = [NSScanner scannerWithString:greenStr];
unsigned int greenIntValue;
[greenScanner scanHexInt:&greenIntValue];
NSString * blueStr = [colorStr substringWithRange:NSMakeRange(4, 2)];
NSScanner * blueScanner = [NSScanner scannerWithString:blueStr];
unsigned int blueIntValue;
[blueScanner scanHexInt:&blueIntValue];
return [UIColor colorWithRed:redIntValue/255.0 green:greenIntValue/255.0 blue:blueIntValue/255.0 alpha:1];
}
多数用于服务器后端传值,然后前端将16进制的色值展示出来