Return to   |   Check out   |   How to Post Snippets  

Not registered? Thats okay! You can Submit a Snippet without being a member of this site!


I find it easier to use html hex colors instead of converting RGB to UIColor blah blah blah. So here is a sweet little snippet to do just that. This piece of code allows me to use Photoshop or any other software with a color picker to get a specific color and use it in my apps, for example, the tintColor of the UINavigationBar.

+(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:@"0X"]) cString = [cString substringFromIndex:2];

    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)

How to use:
So the following example would set 4891CC as the background (or tintColor) for the UINavigationBar

    [self.navigationBar setTintColor: [AppDelegate colorWithHexString:@"4891CC"]];

Update: Aug. 21, 2012

Here is another example I came across on how to use hex strings for UIColor:


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

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

 * Usage
self.view.backgroundColor = UIColorFromRGB(0xCECECE);
self.view.backgroundColor = UIColorFromRGBWithAlpha(0xCECECE, 0.8);

2 Responses to “UIColor from hex strings”

  1. I have to tell you that it’s hard to find
    your articles in google, i found this one on 14 spot, you
    should build some quality backlinks in order to rank your webiste, i know how
    to help you, just search in google – k2 seo tips and

  2. I see a lot of interesting posts on your page. You have to spend a lot of time writing, i know how to save you a lot of work, there is a tool that creates unique,
    google friendly articles in couple of seconds, just search in google – k2 unlimited content

Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

© Copyright 2011 Wright's Computer Services.   All rights reserved Suffusion theme by Sayontan Sinha