新建一個(gè)類命名為UIColor+Hex返吻,在其中添加類方法實(shí)現(xiàn)擴(kuò)展究反。
在.h文件中
寫一個(gè)類方法
+ (UIColor *)colorForHex:(NSString *)hexColor;
.m文件中實(shí)現(xiàn)
@implementation UIColor (Utils)
+ (UIColor *)colorForHex:(NSString *)hexColor{
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [hexColor substringWithRange:range];
range.location = 2;
NSString *gString = [hexColor substringWithRange:range];
range.location = 4;
NSString *bString = [hexColor substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?green:((float) g / 255.0f)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?blue:((float) b / 255.0f)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?alpha:1.0f];
}
@end
在其他類中如果使用追逮,首先包含頭文件蜀铲,然后直接調(diào)用郑象。
例如tabBar.tintColor = [UIColor colorForHex:@"26a6d7"];