您好我想使用HTML颜色代码动态显示UITableviewcells
背景颜色。我的要求是使用json数组并获取HTML Color代码。并使用颜色代码如何转换rgb值。任何身体帮助我。
这是我的json
回复。
[{"classified_id":83,color_code":"#990066}
{"classified_id":82 "color_code":"#ff66cc”}
{"classified_id":80 color_code":"#990066”}
{"classified_id":78 "color_code":"#ff6633”}
{“classified_id":77 "color_code":"#ff6633”}
答案 0 :(得分:1)
您的问题实际上应该如何将十六进制颜色代码转换为UIColor 吧?
在imports
#define UIColorFromRGB(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]
然后你可以得到这样的颜色:
- (UIColor *)colorFromHex:(NSString *)strcolor
{
NSScanner *scanner = [NSScanner scannerWithString:[strcolor stringByReplacingOccurrencesOfString:@"#" withString:@""]];
unsigned hex;
BOOL success = [scanner scanHexInt:&hex];
return UIColorFromRGB(hex);
}
希望你想要的东西。
[self.view setBackgroundColor:[self colorFromHex:@"#FF0000"]];
答案 1 :(得分:0)
尝试这个
#define UIColorFromRGBWithAlpha(rgbValue,a) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]
UIColorFromRGBWithAlpha(0xff6633,1)
您可以在班级中添加方法并将十六进制转换为颜色
-(UIColor *)getUIColorObjectFromHexString:(NSString *)hexStr alpha:(CGFloat)alpha{
unsigned int hexint = [CommonMethods intFromHexString:hexStr];
UIColor *color =
[UIColor colorWithRed:((CGFloat) ((hexint & 0xFF0000) >> 16))/255
green:((CGFloat) ((hexint & 0xFF00) >> 8))/255
blue:((CGFloat) (hexint & 0xFF))/255
alpha:alpha];
return color;
}
-(unsigned int)intFromHexString:(NSString *)hexStr{
unsigned int hexInt = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexStr];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
[scanner scanHexInt:&hexInt];
return hexInt;
}
答案 2 :(得分:0)
您可以使用convenient category to extend UIColor执行此操作。具体来说,您应该使用colorWithHexString:
类方法初始化带有十六进制值的UIColor。
答案 3 :(得分:0)
您可以添加UIColor
类别并添加此方法 -
+ (UIColor*)colorWithHexString:(NSString*)hex
{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) return [UIColor grayColor];
// strip 0X if it appears
if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
if ([cString length] != 6) return [UIColor grayColor];
// 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];
}