2014-01-16 61 views
0

嗨HTML顏色標記我想用HTML代碼的顏色在動態UITableviewcells背景顏色來顯示。我的要求是使用json數組並獲取HTML顏色代碼。並使用顏色代碼如何轉換rgb值。任何身體幫助我。 這是我json響應。如何顯示在UITableviewcells

[{"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

看看這個問題。 http://stackoverflow.com/questions/3805177/how-to-convert-hex-rgb-color-codes-to-uicolor –

回答

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; 
} 
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]; 
} 
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"]]; 
+0

編輯,請檢查 –

相關問題