I’m trying to create a custom color out of a hex color code. I have a seperate class called UIColor+Hex, that takes in a hex string and converts it to a color code and returs a UIColor.
UIColor+Hex.h
#import <UIKit/UIKit.h>
@interface UIColor (Hex)
+ (UIColor *)colorWithHexString:(NSString *)hex;
@end
UIColor+Hex.m
#import "UIColor+Hex.h"
@implementation UIColor (Hex)
+ (UIColor *)colorWithHexString:(NSString *)hex
{
if ([hex length]!=6 && [hex length]!=3)
{
return nil;
}
NSUInteger digits = [hex length]/3;
CGFloat maxValue = (digits==1)?15.0:255.0;
NSUInteger redHex = 0;
NSUInteger greenHex = 0;
NSUInteger blueHex = 0;
sscanf([[hex substringWithRange:NSMakeRange(0, digits)] UTF8String], "%x", &redHex);
sscanf([[hex substringWithRange:NSMakeRange(digits, digits)] UTF8String], "%x", &greenHex);
sscanf([[hex substringWithRange:NSMakeRange(2*digits, digits)] UTF8String], "%x", &blueHex);
CGFloat red = redHex/maxValue;
CGFloat green = greenHex/maxValue;
CGFloat blue = blueHex/maxValue;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
@end
I then have UIColor+Hex.h imported to one of my other classes and make a call to it by:
[self setSelectedfillColor:[UIColor colorWithHexString:@"FF0000"].CGColor];
Whenever I hit this code I get this error…
‘NSInvalidArgumentException’, reason: ‘+[UIColor colorWithHexString:]: unrecognized selector sent to class 0x873d60’
I’ve tried everything I could think of but I still keep getting that error. Anyone have any ideas why this would be happening?
Tested your category as you posted and it works fine even with the cgcolor property method. Separate those two lines and make sure the problem is not with setSelectedFillColor. Sometimes the debug errors can be misleading.