简体   繁体   English

从NSString获取UIColor类型的#define

[英]Get #define of type UIColor from NSString

I have defined the following constants, each of which represents a UIColor: 我定义了以下常量,每个常量代表一个UIColor:

// Purple
#define PURPLE_COLOR_DARK  [UIColor colorWithRed: 0.42 green: 0.25 blue: 0.60 alpha: 1.0]
#define PURPLE_COLOR_LIGHT [UIColor colorWithRed: 0.78 green: 0.47 blue: 0.72 alpha: 1.0]

// Blue
#define BLUE_COLOR_DARK  [UIColor colorWithRed: 0.00 green: 0.46 blue: 0.70 alpha: 1.0]
#define BLUE_COLOR_LIGHT [UIColor colorWithRed: 0.00 green: 0.75 blue: 0.88 alpha: 1.0]

// Magenta
#define MAGENTA_COLOR_DARK  [UIColor colorWithRed: 0.75 green: 0.00 blue: 0.28 alpha: 1.0]
#define MAGENTA_COLOR_LIGHT [UIColor colorWithRed: 1.00 green: 0.32 blue: 0.61 alpha: 1.0]

Question

How can I use an NSString like @"purple" , @"blue" or @"magenta" to get the related "X_COLOR_DARK" or "X_COLOR_LIGHT" constant? 如何使用像@"purple"@"blue"@"magenta"这样的NSString来获取相关的“X_COLOR_DARK”或“X_COLOR_LIGHT”常量? Is it even possible to do so directly? 甚至可以直接这样做吗?

Example

This is wrong, but should provide a clearer picture of what I'm asking. 这是错误的,但应该更清楚地了解我的要求。 In this example, I have the string @"blue" and want to derive the BLUE_COLOR_DARK and BLUE_COLOR_LIGHT color values. 在这个例子中,我有字符串@"blue"并希望派生BLUE_COLOR_DARKBLUE_COLOR_LIGHT颜色值。

NSString *color = @"blue";
UIColor *darkColor = (UIColor *)[[NSString stringWithFormat: @"%@_COLOR_DARK", color] uppercaseString];

// Debugging    
NSLog(@"%@", darkColor); // BLUE_COLOR_DARK
NSLog(@"%hhd", [lightColor isKindOfClass: [UIColor class]]);  // 0
NSLog(@"%hhd", [lightColor isKindOfClass: [NSString class]]); // 1

Follow-Up 跟进

Note that UIColor is not necessarily relevant to answering the question, though it does help for my situation. 请注意,UIColor不一定与回答问题相关,尽管它对我的情况有帮助。 More specifically, I'm asking if a string matching a defined name (eg @"PURPLE_COLOR_DARK" and #define PURPLE_COLOR_DARK ) can be interpreted as the defined name instead of using the defined name directly. 更具体地说,我问的是匹配定义名称的字符串(例如@"PURPLE_COLOR_DARK"#define PURPLE_COLOR_DARK )是否可以解释为定义的名称,而不是直接使用定义的名称。

Ditch the #defines. 放弃#defines。 And use a UIColor category and NSSelectorFromString like outlined in this answer . 并使用UIColor类别和NSSelectorFromString本答案中所述

Or, create a NSDictionary where the keys are the names of your colors and the objects are your colors. 或者,创建一个NSDictionary ,其中键是颜色的名称,对象是您的颜色。 You can then get UIColor by name. 然后,您可以按名称获取UIColor

Eg: 例如:

NSDictionary *colors = @{ @"purple_dark"  : PURPLE_COLOR_DARK,
                          @"purple_light" : PURPLE_COLOR_LIGHT };

NSString *colorName = @"purple";

NSString *lightColorName = [colorName stringByAppendingString:@"_light"];
NSString *darkColorName = [colorName stringByAppendingString:@"_dark"];

UIColor *lightColor = colors[lightColorName];
UIColor *darkColor = colors[darkColorName];

you can't use the #defined names directly because those names don't exist in the compiled code. 您不能直接使用#defined名称,因为这些名称在编译的代码中不存在。 During compilation the preprocessor will replace all occurrences of PURPLE_COLOR_DARK with [UIColor ...] 在编译期间,预处理器将用[UIColor ...]替换所有出现的PURPLE_COLOR_DARK

You can create category of UIColor. 您可以创建UIColor的类别。

UIColor+CustomColors.h 的UIColor + CustomColors.h

+ (UIColor *)redFill;

UIColor+CustomColors.m 的UIColor + CustomColors.m

+ (UIColor *)redFill
{
    return [UIColor colorWithRed:240.0 / 255.0 green:36.0 / 255.0 blue:0.0 / 255.0 alpha:1.0];
}

Now use this category in your class: 现在在您的班级中使用此类别:

NSArray *color = @[[UIColor redFill], ...etc];

Understand category 理解类别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM