Search code examples
iosios4

How to load a UIColor from a Plist


Regarding saving the UIColor in a Plist: I have tried different ways but not been able to do so, I want to save and retrieve the color values in a plist file.

I can not extract the data value of the color using nslog and save it in the plist.

Is there any other way to do so?


Solution

  • I prefer using string to store the color. The parsing code that does this shown below (cut out from https://github.com/xslim/TKThemeManager/blob/master/TKThemeManager.m#L162)

    + (UIColor *)colorFromString:(NSString *)hexString {    
        NSScanner *scanner = [NSScanner scannerWithString:hexString];
        unsigned hex;
        BOOL success = [scanner scanHexInt:&hex];
    
        if (!success) return nil;
        if ([hexString length] <= 6) {
            return UIColorFromRGB(hex);
        } else {
            unsigned color = (hex & 0xFFFFFF00) >> 8;
            CGFloat alpha = 1.0 * (hex & 0xFF) / 255.0;
            return UIColorFromRGBA(color, alpha);
        }
    }