#pragma mark - 颜色转换 ios中十六进制的颜色转换为uicolor
(uicolor *) colorwithhexstring: (nsstring *)color
{
nsstring *cstring = [[color stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]] uppercasestring];
// string should be 6 or 8 characters
if ([cstring length] < 6) {
return [uicolor clearcolor];
}
// strip 0x if it appears
if ([cstring hasprefix:@"0x"])
cstring = [cstring substringfromindex:2];
if ([cstring hasprefix:@"#"])
cstring = [cstring substringfromindex:1];
if ([cstring length] != 6)
return [uicolor clearcolor];
// separate into r, g, b substrings
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];
// scan values
unsigned int r, g, b;
[[nsscanner scannerwithstring:rstring] scanhexint:&r];
[[nsscanner scannerwithstring:gstring] scanhexint:&g];
[[nsscanner scannerwithstring:bstring] scanhexint:&b];
return [uicolor colorwithred:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}
用户登录
还没有账号?立即注册
用户注册
投稿取消
| 文章分类: |
|
还能输入300字
上传中....
徐州小霸王