Created
December 18, 2014 09:54
-
-
Save ThinhPhan/e34f701586d947c7a941 to your computer and use it in GitHub Desktop.
Objective-c UIColor category get color from hex string.
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
// | |
// UIColor+HexString.h | |
// DemoMapviewScrollView | |
// | |
// Created by ThinhPhan on 12/18/14. | |
// Copyright (c) 2014 Gennova. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIColor (HexString) | |
/*! | |
* @author Thinh Phan, 14-12-18 16:12:14 | |
* | |
* @ref http://stackoverflow.com/a/3805354 | |
* | |
* @brief Change from hex string to UIColor | |
* | |
* @param hexString string format "#abc", "#abcdef31" or "RRGGBB" | |
* | |
* @return UIColor object. | |
*/ | |
+ (UIColor *)colorFromHexString:(NSString *)hexString; | |
/*! | |
* @author Thinh Phan, 14-12-18 16:12:12 | |
* | |
* @brief Convert from hex string to UIColor object. | |
* | |
* @param hexString string format : #RBG, #ARGB ##RRGGBB or #AARRGGBB. | |
* | |
* @return UIColor object. | |
*/ | |
+ (UIColor *)colorWithHexString:(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
// | |
// UIColor+HexString.m | |
// DemoMapviewScrollView | |
// | |
// Follow http://stackoverflow.com/a/7180905 | |
// Created by ThinhPhan on 12/18/14. | |
// Copyright (c) 2014 Gennova. All rights reserved. | |
// | |
#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]; | |
} | |
+ (UIColor *)colorWithHexString:(NSString *)hexString { | |
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString]; | |
CGFloat alpha, red, blue, green; | |
switch ([colorString length]) { | |
case 3: // #RGB | |
alpha = 1.0f; | |
red = [self colorComponentFrom: colorString start: 0 length: 1]; | |
green = [self colorComponentFrom: colorString start: 1 length: 1]; | |
blue = [self colorComponentFrom: colorString start: 2 length: 1]; | |
break; | |
case 4: // #ARGB | |
alpha = [self colorComponentFrom: colorString start: 0 length: 1]; | |
red = [self colorComponentFrom: colorString start: 1 length: 1]; | |
green = [self colorComponentFrom: colorString start: 2 length: 1]; | |
blue = [self colorComponentFrom: colorString start: 3 length: 1]; | |
break; | |
case 6: // #RRGGBB | |
alpha = 1.0f; | |
red = [self colorComponentFrom: colorString start: 0 length: 2]; | |
green = [self colorComponentFrom: colorString start: 2 length: 2]; | |
blue = [self colorComponentFrom: colorString start: 4 length: 2]; | |
break; | |
case 8: // #AARRGGBB | |
alpha = [self colorComponentFrom: colorString start: 0 length: 2]; | |
red = [self colorComponentFrom: colorString start: 2 length: 2]; | |
green = [self colorComponentFrom: colorString start: 4 length: 2]; | |
blue = [self colorComponentFrom: colorString start: 6 length: 2]; | |
break; | |
default: | |
[NSException raise:@"Invalid color value" | |
format: @"Color value %@ is invalid. It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString]; | |
break; | |
} | |
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha]; | |
} | |
#pragma mark - Helper Methods | |
+ (CGFloat)colorComponentFrom:(NSString *)string start:(NSUInteger)start length:(NSUInteger)length { | |
NSString *substring = [string substringWithRange: NSMakeRange(start, length)]; | |
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring]; | |
unsigned hexComponent; | |
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent]; | |
return hexComponent / 255.0; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment