开发者

Converting hex value from string to hext color code

In Objective-C xcode project I have a plist file with which associates integers with hex-color codes. Dynamically I want to use this color-code from plist file and pass that hex value to the following macro to get the UIColor object.

Macro:

#define UIColorFromRGB(rgbValue) [UIColor \
 colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

My actual hex value which I need to pass to this mac开发者_StackOverflowro is 0xF2A80C, but it is present in the plist file. I can get this as a string. How should I do in this case?

Thanks in advance.

Do you want any details regarding this?.


NSScanner *scanner = [NSScanner scannerWithString:hexString];
unsigned hex;
BOOL success = [scanner scanHexInt:&hex];
UIColor *color = UIColorFromRGB(hex);


What about this:

NSString *textValue = @"0xF2A80C";
long long value = [textValue longLongValue];
UIColor *color = UIColorFromRGB(value);

I've not tested it. So report me if there are problems.


You can use the good old C function strtol with 16 as base.

const char* cString = [myStr cStringUsingEncoding:[NSString defaultCStringEncoding]];
int hexValue = (int)strtol(cString, NULL, 16);
UIColor *color = UIColorFromRGB(hexValue);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜