通过十六进制字符串 转化成十进制的整数,然后初始化一个UIColor对象
- (UIColor *)colorWithHexString:(NSString *)hexString
{
//分割RGB
NSString *red = [hexString substringWithRange:NSMakeRange(0, 2)];
NSString *green = [hexString substringWithRange:NSMakeRange(2, 2)];
NSString *blue = [hexString substringWithRange:NSMakeRange(4, 2)];
unsigned int R = 0;
unsigned int G = 0;
unsigned int B = 0;
//NSScanner 是一个扫描类,可以将字符串中的数转为数字.
//scanHexInt 将字符串中的十六进制数转为十进制的整数
[[NSScanner scannerWithString:red] scanHexInt:&R];
[[NSScanner scannerWithString:green] scanHexInt:&G];
[[NSScanner scannerWithString:blue] scanHexInt:&B];
return [UIColor colorWithRed:R / 255.0 green:G / 255.0 blue:B / 255.0 alpha:1];
}