Created
November 30, 2011 00:04
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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