How can I create a UIColor
from a hexadecimal string format, such as #00FF00
?
Use this Category :
in the file UIColor+Hexadecimal.h
#import
@interface UIColor(Hexadecimal)
+ (UIColor *)colorWithHexString:(NSString *)hexString;
@end
in the file UIColor+Hexadecimal.m
#import "UIColor+Hexadecimal.h"
@implementation UIColor(Hexadecimal)
+ (UIColor *)colorWithHexString:(NSString *)hexString {
unsigned rgbValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&rgbValue];
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}
@end
In Class you want use it :
#import "UIColor+Hexadecimal.h"
and:
[UIColor colorWithHexString:@"#6e4b4b"];