針對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];
這樣也是可以設置相應控件顏色的!