通常UI給我們的色值都是16進制,我們需要進行轉(zhuǎn)換
#import@interface UIColor (NSString)
/**
*? 16進制字符串轉(zhuǎn)顏色
*
*? @param hexColor 16進制字符串
*
*? @return 返回顏色
*/
+ (UIColor *)getColor:(NSString *)hexColor;
@end
實現(xiàn):
#pragma -mark GetColor//解析16進制的顏色
+ (UIColor *)getColor:(NSString *)hexColor {
unsigned int red,green,blue;
NSRange range;
range.length = 2;
range.location = 0;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
range.location = 2;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
range.location = 4;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
return RGBACOLOR(red, green, blue, 1.0);
}