从字符串或16进制数得到UIColor
2015-03-24 11:26
127 查看
+(UIColor*)GetColorFromCSSHex:(NSString *) hexColor{ // @"#FF3300"
if (hexColor == nil || [hexColor isEqualToString:@""]) {
return nil;
}
if ([hexColor length] != 7) {
return nil;
}
//扫描十六进制字符串
unsigned int red = 255, green = 255, blue = 255;
NSRange range;
range.length = 2;
range.location = 1;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];//扫描一个十六进制表示的无符号值且取出并转换成十进制存储在red中
range.location = 3;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
range.location = 5;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:1.0f];
}
+(UIColor*)GetColorFromHexnum:(NSInteger)hexColor //0xFF3300
{
Byte red = hexColor>>16;
Byte green = hexColor>>8;
Byte blue = hexColor;
return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:1.0f];
}
if (hexColor == nil || [hexColor isEqualToString:@""]) {
return nil;
}
if ([hexColor length] != 7) {
return nil;
}
//扫描十六进制字符串
unsigned int red = 255, green = 255, blue = 255;
NSRange range;
range.length = 2;
range.location = 1;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];//扫描一个十六进制表示的无符号值且取出并转换成十进制存储在red中
range.location = 3;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
range.location = 5;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:1.0f];
}
+(UIColor*)GetColorFromHexnum:(NSInteger)hexColor //0xFF3300
{
Byte red = hexColor>>16;
Byte green = hexColor>>8;
Byte blue = hexColor;
return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:1.0f];
}
相关文章推荐
- 16进制颜色(html颜色值)字符串转为UIColor
- 16进制颜色(html颜色值)字符串转为UIColor
- iOS开发中16进制颜色(html颜色值)字符串转为UIColor
- 16进制颜色字符串转为UIColor
- UIColor的16进制色值,16进制字符串扩展
- 16进制颜色(html颜色值)字符串转为UIColor
- 16进制颜色(html颜色值)字符串转为UIColor
- 16进制颜色(html颜色值)字符串转为UIColor【RGB】
- RGB颜色转换_16进制颜色(html颜色值)字符串转为UIColor
- ios开发之16进制颜色(html颜色值)字符串转为UIColor
- 16进制颜色(html颜色值)字符串转为UIColor
- 16进制颜色(html颜色值)字符串转为UIColor
- iOS开发中16进制颜色(html颜色值)字符串转为UIColor
- 16进制颜色(html颜色值)字符串转为UIColor
- IOS 16进制颜色字符串转UIColor
- ios开发之16进制颜色(html颜色值)字符串转为UIColor
- 16进制颜色(html颜色值)字符串转为UIColor
- 16进制颜色(html颜色值)字符串转为UIColor
- iOS 16进制颜色字符串转换成可以正常使用的UIColor