总结几个UIColor 的技巧:
/**
@brief 根据r,g,b的值(0~255)创建UIColor
*/
#define UIColorWithRGB(r, g, b) [UIColor colorWithRed:(r) / 255.0f green:(g) / 255.0f blue:(b) / 255.0f alpha:1.0f]
/**
@brief 根据r,g,b,a的值(0~255)创建UIColor
*/
#define UIColorWithRGBA(r, g, b, a) [UIColor colorWithRed:(r) / 255.0f green:(g) / 255.0f blue:(b) / 255.0f alpha:(a) / 255.0f]
/**
@brief 根据十六进制创建UIColor
*/
#define UIColorFromHex(s) [UIColor colorWithRed:(((s & 0xFF0000) >> 16))/255.0green:(((s &0xFF00) >>8))/255.0blue:((s &0xFF))/255.0alpha:1.0]
通过这个程序就能明白:
- (UIColor *) stringTOColor:(NSString *)str
{
if (!str || [str isEqualToString:@""]) {
return nil;
}
unsigned red,green,blue;
NSRange range;
range.length = 2;
range.location = 1;
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
range.location = 3;
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
range.location = 5;
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
return color;
}
其实RGB就是每两位的十六进制来分别表示,很明显,UIColor的十六进制和RGB其实就是一回事,计算机科学也非常强大,能用数理去统一它
参考:
http://blog.csdn.net/youcanping2008/article/details/9207431
http://www.jianshu.com/p/d3fd0862e843