0

Hi i want to display in UITableviewcells background color dynamically using HTML Color codes. My requirement is using json array and get HTML Color codes. And using color codes how to convert rgb value.Any body help me. This is my json response.

[{"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”}
Sport
  • 8,570
  • 6
  • 46
  • 65
user2930730
  • 359
  • 2
  • 4
  • 14
  • Take a look at this question. http://stackoverflow.com/questions/3805177/how-to-convert-hex-rgb-color-codes-to-uicolor – Brian Tracy Jan 16 '14 at 05:46

4 Answers4

1

Your question should actually be How to convert Hex color code to UIColor right?

create this macro on top of your class after 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]

Then you can get color like this:

- (UIColor *)colorFromHex:(NSString *)strcolor
{
    NSScanner *scanner = [NSScanner scannerWithString:[strcolor stringByReplacingOccurrencesOfString:@"#" withString:@""]];
    unsigned hex;
    BOOL success = [scanner scanHexInt:&hex];
    return UIColorFromRGB(hex);
}

Hope its what you want.

[self.view setBackgroundColor:[self colorFromHex:@"#FF0000"]];
Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75
0

try this one

  #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)

you can add method in your class and convert hex to color

-(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;
}
nitin kachhadiya
  • 959
  • 2
  • 9
  • 21
0

You can do this by using a convenient category to extend UIColor. Specifically, you should use the colorWithHexString: class method to initialize a UIColor with your hex value.

Patrick Goley
  • 5,397
  • 22
  • 42
0

You can add UIColor Category and add this method -

+ (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];
}
Rahul Mane
  • 1,005
  • 18
  • 33