用法:
在要用到的类里倒入头文件(或者在pch里倒入也行),然后调用类方法即可。
cell.contentView.backgroundColor =[UIColor colorWithHexString:@"#3579f4"];
.h文件:
@interface UIColor (UIcolor_RGB)
+ (UIColor*)colorWithHexString:(NSString*)stringToConvert;
@end
.m文件:
#import "UIColor+UIcolor_RGB.h"
@implementation UIColor (UIcolor_RGB)
+ (UIColor*)colorWithHexString:(NSString*)stringToConvert{
if([stringToConvert hasPrefix:@"#"])
{
stringToConvert = [stringToConvert substringFromIndex:1];
}
NSScanner*scanner = [NSScanner scannerWithString:stringToConvert];
unsigned hexNum;
if(![scanner scanHexInt:&hexNum])
{
return nil;
}
return[UIColor colorWithRGBHex:hexNum];
}
+ (UIColor*)colorWithRGBHex:(UInt32)hex{
int r = (hex >>16) &0xFF;
int g = (hex >>8) &0xFF;
int b = (hex) &0xFF;
return[UIColor colorWithRed:r /255.0f
green:g /255.0f
blue:b /255.0f
alpha:1.0f];
}
@end