iOS 16进制字符串转颜色

通常UI给我们的色值都是16进制,我们需要进行转换

#import@interface UIColor (NSString)

/**

*  16进制字符串转颜色

*

*  @param hexColor 16进制字符串

*

*  @return 返回颜色

*/

+ (UIColor *)getColor:(NSString *)hexColor;

@end



实现:

#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);

}

你可能感兴趣的:(iOS 16进制字符串转颜色)