Created
January 10, 2015 20:18
-
-
Save dminones/b2d6bfeab5a97ac45022 to your computer and use it in GitHub Desktop.
Objective C Category: hex NString to UIColor
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+ColorFromHexa.h | |
// TellApp | |
// | |
// Created by Dario on 1/10/15. | |
// Copyright (c) 2015 Cactus. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface UIColor (CustomColorCatagory) //This line is one of the most important ones - it tells the complier your extending the normal set of methods on UIColor | |
+ (UIColor *)colorFromHexaString:(NSString*)hexaString; | |
@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+ColorFromHexa.m | |
// TellApp | |
// | |
// Created by Dario on 1/10/15. | |
// Copyright (c) 2015 Cactus. All rights reserved. | |
// | |
#import "UIColor+ColorFromHexa.h" | |
@implementation UIColor (ColorFromHexa) | |
+ (UIColor *)colorFromHexaString:(NSString*)hexaString | |
{ | |
NSString *prefixToRemove = @"#"; | |
NSString *serializedColor = hexaString; | |
if ([hexaString hasPrefix:prefixToRemove]) { | |
serializedColor = [hexaString substringFromIndex:[prefixToRemove length]]; | |
} | |
NSScanner *scanner2 = [NSScanner scannerWithString:serializedColor]; | |
unsigned int baseColor1; | |
[scanner2 scanHexInt:&baseColor1]; | |
CGFloat red = ((baseColor1 & 0xFF0000) >> 16) / 255.0f; | |
CGFloat green = ((baseColor1 & 0x00FF00) >> 8) / 255.0f; | |
CGFloat blue = (baseColor1 & 0x0000FF) / 255.0f; | |
UIColor *color = [UIColor colorWithRed:(red) | |
green:(green) | |
blue:(blue) | |
alpha:1]; | |
return color; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment