您当前位置:
首页 >
互联网 > 颜色(color)转换为三色值(r/g/b)(干货分享)
颜色(color)转换为三色值(r/g/b)(干货分享)
来源:程序员人生 发布时间:2014-10-04 08:00:01 阅读次数:3729次
//颜色转换 ##665522 ― 三色值
+ (UIColor *)setFontColorWithString:(NSString *)color
{
NSString *cString
= [[color stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]] uppercaseString];
// 字符串个数必须大于等于6
if ([cString length] < 6) {
return [UIColorclearColor];
}
// 根据16进制来区分
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColorclearColor];
// 分为r,g,b 子字符串
NSRange range;
range.location = 0;
range.length = 2;
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = 2;
NSString *gString = [cString substringWithRange:range];
//b
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// 扫描r,g,b值
unsignedint r, g, b;
[[NSScannerscannerWithString:rString] scanHexInt:&r];
[[NSScannerscannerWithString:gString] scanHexInt:&g];
[[NSScannerscannerWithString:bString] scanHexInt:&b];
return RgbHex2UIColor(r, g, b);
}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
------分隔线----------------------------
------分隔线----------------------------