Last active
August 29, 2015 14:12
-
-
Save andrey-str/28034ae35a1ddb2e29d1 to your computer and use it in GitHub Desktop.
Convert hex color to UIColor
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 | |
// Gists | |
// | |
// Created by Andrey Streltsov on 24 Dec 2014. | |
// Copyright (c) 2014 Scopicsoftware. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIColor (Hex) | |
+ (UIColor *)colorFromHexString:(NSString *)hexString; | |
@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.mm | |
// Gists | |
// | |
// Created by Andrey Streltsov on 24 Dec 2014. | |
// Copyright (c) 2014 Scopicsoftware. All rights reserved. | |
// | |
#import "UIColor+Hex.h" | |
@implementation UIColor (Hex) | |
+ (UIColor *)colorFromHexString:(NSString *)hexString { | |
// Check for hash and add the missing hash | |
if('#' != [hexString characterAtIndex:0]) | |
{ | |
hexString = [NSString stringWithFormat:@"#%@", hexString]; | |
} | |
// check for string length | |
NSAssert(7 == hexString.length || 4 == hexString.length, @"check for string length"); | |
// check for 3 character HexStrings | |
hexString = [self hexStringTransformFromThreeCharacters:hexString]; | |
NSString *redHex = [NSString stringWithFormat:@"0x%@", [hexString substringWithRange:NSMakeRange(1, 2)]]; | |
unsigned redInt = [self hexValueToUnsigned:redHex]; | |
NSString *greenHex = [NSString stringWithFormat:@"0x%@", [hexString substringWithRange:NSMakeRange(3, 2)]]; | |
unsigned greenInt = [self hexValueToUnsigned:greenHex]; | |
NSString *blueHex = [NSString stringWithFormat:@"0x%@", [hexString substringWithRange:NSMakeRange(5, 2)]]; | |
unsigned blueInt = [self hexValueToUnsigned:blueHex]; | |
UIColor *color = [UIColor colorWith8BitRed:redInt green:greenInt blue:blueInt alpha:1.0]; | |
return color; | |
} | |
+ (UIColor *)colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha | |
{ | |
UIColor *color = nil; | |
color = [UIColor colorWithRed:(float)red/255 green:(float)green/255 blue:(float)blue/255 alpha:alpha]; | |
return color; | |
} | |
+ (NSString *)hexStringTransformFromThreeCharacters:(NSString *)hexString | |
{ | |
if(hexString.length == 4) | |
{ | |
hexString = [NSString stringWithFormat:@"#%@%@%@%@%@%@", | |
[hexString substringWithRange:NSMakeRange(1, 1)],[hexString substringWithRange:NSMakeRange(1, 1)], | |
[hexString substringWithRange:NSMakeRange(2, 1)],[hexString substringWithRange:NSMakeRange(2, 1)], | |
[hexString substringWithRange:NSMakeRange(3, 1)],[hexString substringWithRange:NSMakeRange(3, 1)]]; | |
} | |
return hexString; | |
} | |
+ (unsigned)hexValueToUnsigned:(NSString *)hexValue | |
{ | |
unsigned value = 0; | |
NSScanner *hexValueScanner = [NSScanner scannerWithString:hexValue]; | |
[hexValueScanner scanHexInt:&value]; | |
return value; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment