Created
June 15, 2011 07:37
-
-
Save sadatrahman/1026653 to your computer and use it in GitHub Desktop.
UIColor factory category method - returns an appropriate UIColor instance given a hex colour code.
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
// | |
// UIColor+Hex.h | |
// SRKit | |
// | |
// Created by Sadat Rahman on 29/11/08. | |
// Copyright Sadat Rahman 2008. All rights reserved. | |
// | |
@interface UIColor (Hex) | |
+ (UIColor *)colorWithHexValue:(NSString *)hexValue; | |
@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
// | |
// UIColor+Hex.m | |
// SRKit | |
// | |
// Created by Sadat Rahman on 29/11/08. | |
// Copyright Sadat Rahman 2008. All rights reserved. | |
// | |
#import "UIColor+Hex.h" | |
@implementation UIColor (Hex) | |
+ (UIColor *)colorWithHexValue:(NSString *)hexValue | |
{ | |
NSString *cleanedHexValue = [hexValue stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"#"]]; | |
unsigned long rgbColorValue = strtoul([[cleanedHexValue substringToIndex:6] cStringUsingEncoding:NSASCIIStringEncoding], NULL, 16); | |
NSUInteger redValue = (rgbColorValue >> 16) & 0xFF; | |
NSUInteger greenValue = (rgbColorValue >> 8) & 0xFF; | |
NSUInteger blueValue = (rgbColorValue >> 0) & 0xFF; | |
return [UIColor colorWithRed:(redValue / 255.) green:(greenValue / 255.) blue:(blueValue / 255.) alpha:1.]; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment