Return to WrightsCS.com   |   Check out WrightsCSApps.com   |   How to Post Snippets  

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


          0 votes

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)
                           alpha:1.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:

Source: http://cocoamatic.blogspot.com/2010/07/uicolor-macro-with-hex-values.html

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

Leave a Reply

(required)

(required)

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=""> <strike> <strong>

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