How to download UIColor from Plist

Regarding saving UIColor to Plist: I tried different methods, but could not do it, I want to save and get the color values ​​in the plist file.

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

Is there any other way to do this?

+8
ios ios4
source share
4 answers

I prefer to use a string to preserve color. The parsing code that shows this below (cut 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); } } 
+7
source share

For a quick fix (but maybe not the prettiest one):

  • Add color property as type Number to plist
  • Enter the color as RGB hexadecimal, for example: 0xff00e3
  • Read this and process it with a macro as shown below.

Here is a sample code:

 // 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 hexadecimal, the plist editor will show it as a decimal number. Not good. As a developer, as a rule, you copy all the colors from a project document, so the need to read the color values ​​is not so great.

+3
source share

I made 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 used by NSStringFromCGAffineTransform. This is actually part of the representative of the larger plist object in [eppz! Kit on GitHub] [1].

+1
source share

The 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 receiving the string from UIColor, the task should be simple enough to save it in the plist file and retrieve it later.

0
source share

Source: https://habr.com/ru/post/650146/


All Articles