How to load a UIColor from a Plist
Regarding saving the UIColor in a Plist: I have trie开发者_如何学Pythond 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?
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);
}
}
For a quick solution (but maybe not the most pretty one):
- Add the color property as a type Number into the plist
- Enter the color as an RGB-hexdecimal, for example:
0xff00e3
- Read it out and process it with a macro like below
Here is a code example:
// Add this code to some include, for reuse
#define UIColorFromRGBA(rgbValue, alphaValue) ([UIColor colorWithRed:((CGFloat)((rgbValue & 0xFF0000) >> 16)) / 255.0 \
green:((CGFloat)((rgbValue & 0xFF00) >> 8)) / 255.0 \
blue:((CGFloat)(rgbValue & 0xFF)) / 255.0 \
alpha:alphaValue])
// This goes into your controller / view
NSDictionary *myPropertiesDict = [NSDictionary dictionaryWithContentsOfFile:...];
UIColor *titleColor = UIColorFromRGBA([myPropertiesDict[@"titleColor"] integerValue], 1.0);
After entering the color as hexdecimal, the plist editor will show it as a decimal number. Not nice. As a developer you normally copy paste the colors from a design document anyway, so the need to read the color values is not that big.
I did a category for this:
@implementation UIColor (EPPZRepresenter)
NSString *NSStringFromUIColor(UIColor *color)
{
const CGFloat *components = CGColorGetComponents(color.CGColor);
return [NSString stringWithFormat:@"[%f, %f, %f, %f]",
components[0],
components[1],
components[2],
components[3]];
}
UIColor *UIColorFromNSString(NSString *string)
{
NSString *componentsString = [[string stringByReplacingOccurrencesOfString:@"[" withString:@""] stringByReplacingOccurrencesOfString:@"]" withString:@""];
NSArray *components = [componentsString componentsSeparatedByString:@", "];
return [UIColor colorWithRed:[(NSString*)components[0] floatValue]
green:[(NSString*)components[1] floatValue]
blue:[(NSString*)components[2] floatValue]
alpha:[(NSString*)components[3] floatValue]];
}
@end
The same formatting that is used by NSStringFromCGAffineTransform. This is actually a part of a bigger scale plist object representer in [eppz!kit at GitHub][1].
Best solution I found for this problem is on the following site:
http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars
After getting the string from a UIColor, it should be an easy enough task to save it to a plist file and retrieve it later on.
精彩评论