Skip to content

Instantly share code, notes, and snippets.

@burtlo
Created November 30, 2011 00:04
Show Gist options
  • Save burtlo/1407264 to your computer and use it in GitHub Desktop.
Save burtlo/1407264 to your computer and use it in GitHub Desktop.
UIColor Category to give it the ability to be created from a hex color as a NSString
#import "UIColor+Hex.h"
#import <UIKit/UIKit.h>
@interface UIColor (HexSupport)
// Provide the ability to generate UIColor from a Hex string value
+ (id)colorWithHex:(NSString *)hex;
@end
@implementation UIColor (HexSupport)
+ (id)colorWithHex:(NSString *)hex
{
NSScanner *scanner = [NSScanner scannerWithString:hex];
unsigned int convertedColor;
[scanner scanHexInt:&convertedColor];
float red = (float) ((convertedColor & 0xff0000) >> 16) / 255.0f;
float green = (float) ((convertedColor & 0x00ff00) >> 8) / 255.0f;
float blue = (float) ((convertedColor & 0x0000ff) >> 0) / 255.0f;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment