如何从HTML颜色格式设置CIColor?例如,我从文件“#e0e0e0”中读取,我需要将此颜色设置为UILable。当我尝试使用[UIColor colorWithRed:green:blue:alpha:1.0]时,我发现它使用浮点数定义颜色%。 如何使用将backgroundColor设置为UILable的LONG颜色格式?
答案 0 :(得分:3)
摘自本指南:http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
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];
答案 1 :(得分:2)
每种颜色都是十六进制数,最大值为0xff(十进制255)。将当前值除以255,您将使用浮点数创建CIColor。