Search code examples
objective-ccocoamacrosplistuicolor

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 macro 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?.


Solution

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