项目中用到某些控件背景需要服务器控制返回值为字符串类似#1B9E5CFF
#define rgba(r,g,b,s) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:s]
/* 16进制转RGB DLog(@"查看颜色:%@",[self getColor:@"#1B9E5C"]); */
+(UIColor *)getSixteenToRGBColor:(NSString *)hexColor {
// [self getSixteenToRGBColor:@"#1B9E5CFF"];//
unsigned int red,green,blue,aphex;
hexColor = [hexColor stringByReplacingOccurrencesOfString:@"#" withString:@""];
if (hexColor.length>=6) {
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];
aphex = 1;
if (hexColor.length==8) {
range.location = 6;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&aphex];
}
DLog(@"RGB:%d-%d-%d-%d",red,green,blue,aphex);
return rgba(red, green, blue, aphex);
}else{
return UIColor.whiteColor;
}
}
网友评论