Created
December 18, 2015 12:40
-
-
Save bigeyex/a4b7ff56cb43f54c4fe1 to your computer and use it in GitHub Desktop.
UIClolor+HexString: Create your UIColor from a "css color code
This file contains 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 <Foundation/Foundation.h> | |
@interface UIColor (HexString) | |
+ (UIColor *) colorFromHexString:(NSString *)hexString; | |
@end |
This file contains 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+HexString.h" | |
@implementation UIColor (HexString) | |
+ (UIColor *) colorFromHexString:(NSString *)hexString { | |
NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""]; | |
if([cleanString length] == 3) { | |
cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@", | |
[cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)], | |
[cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)], | |
[cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]]; | |
} | |
if([cleanString length] == 6) { | |
cleanString = [cleanString stringByAppendingString:@"ff"]; | |
} | |
unsigned int baseValue; | |
[[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue]; | |
float red = ((baseValue >> 24) & 0xFF)/255.0f; | |
float green = ((baseValue >> 16) & 0xFF)/255.0f; | |
float blue = ((baseValue >> 8) & 0xFF)/255.0f; | |
float alpha = ((baseValue >> 0) & 0xFF)/255.0f; | |
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment