首页 > 系统 > iOS > 正文

iOS 关于16进制RGB颜色设置问题

2019-11-09 14:52:13
字体:
来源:转载
供稿:网友

针对UI给的标注图上,色值给的都是16进制的,如#ED145B,#000000等等, 那么我们在代码中应该怎么使用这一色值呢?下面给出几种方案以供参考!

1. 写一个宏定义:

#define UGUColorFromRGB_0x(rgbValue) [UIColor /colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 /green:((float)((rgbValue & 0xFF00) >> 8))/255.0 /blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

使用:

moreLabel.textColor = UGUColorFromRGB_0x(0x666666);

这样就可以为该控件设置字体颜色了

2.自己写一个转换方法 调用

#PRagma mark - 16进制转RGB+ (UIColor *)getColorWithHexStr:(NSString *)hexColor { if([hexColor containsString:@"#"]){ //去掉# hexColor = [hexColor substringFromIndex:1]; } 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 [UIColor colorWithRed:(red)/255.f green:(green)/255.f blue:(blue)/255.f alpha:1.f];}

调用方法:

[_protocolBtn setTitleColor:[BSDTools getColorWithHexStr:@"000000"] forState:UIControlStateNormal];

这样也是可以设置相应控件颜色的!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表