C语言 定义 UIColor 常量的 Objective C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2824187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective C defining UIColor constants
提问by futureelite7
I have a iPhone application with a few custom-defined colors for my theme. Since these colors will be fixed for my UI, I would like to define the colors in a class to be included (Constants.h and Constants.m). How do I do that? (Simply defining them does not work because UIColors are mutable, and would cause errors - Initalizer not constant).
我有一个 iPhone 应用程序,为我的主题设置了一些自定义颜色。由于这些颜色对于我的 UI 来说是固定的,我想在要包含的类中定义颜色(Constants.h 和 Constants.m)。我怎么做?(简单地定义它们是行不通的,因为 UIColors 是可变的,并且会导致错误 - Initalizer 不是常量)。
/* Constants.h */
extern UIColor *test;
/* Constants.m */
UIColor *test = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
Thanks!
谢谢!
回答by drawnonward
A UIColor is not mutable. I usually do this with colors, fonts and images. You could easily modify it to use singletons or have a static initializer.
UIColor 是不可变的。我通常用颜色、字体和图像来做这件事。您可以轻松修改它以使用单例或具有静态初始值设定项。
@interface UIColor (MyProject)
+(UIColor *) colorForSomePurpose;
@end
@implementation UIColor (MyProject)
+(UIColor *) colorForSomePurpose { return [UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0]; }
@end
回答by Fateh Khalsa
For simplicity I did this:
为简单起见,我这样做了:
/* Constants.h */
#define myColor [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]
Don't forget to leave out the ';' so you can use it as a normal expression.
不要忘记省略“;” 因此您可以将其用作正常表达式。
I'm not sure if there's anything technically wrong with this approach, but it works fine, and avoids the compile-time constant initializer error - this code is effectively stuck in place anywhere you put 'myColor', so it doesn't ever get compiled until you actually use it.
我不确定这种方法在技术上是否有任何问题,但它工作正常,并避免了编译时常量初始化错误——这段代码被有效地卡在你放置“myColor”的任何地方,所以它永远不会得到编译直到你实际使用它。
回答by yeahdixon
Another option
另外一个选项
in your .h you can do
在你的 .h 你可以做
extern UIColor * const COLOR_LIGHT_BLUE;
in your .mm you can do
在你的 .mm 你可以做
UIColor* const COLOR_LIGHT_BLUE = [[UIColor alloc] initWithRed:21.0f/255 green:180.0f/255 blue:1 alpha:1];//;#15B4FF
回答by AmitP
回答by goetz
Here's another way:
这是另一种方式:
Header:
标题:
#if !defined(COLORS_EXTERN)
#define COLORS_EXTERN extern
#endif
COLORS_EXTERN UIColor *aGlobalColor;
Implementation:
执行:
#define COLORS_EXTERN
#import "GlobalColors.h"
@interface GlobalColors : NSObject
@end
@implementation GlobalColors
+ (void)load
{
aGlobalColor = [UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1];
}
@end
It's a bit of a hack, but you don't need to redefine the color in the implementation and you can access colors without a method call.
这有点小技巧,但您不需要在实现中重新定义颜色,并且无需调用方法即可访问颜色。
回答by Kendall Helmstetter Gelner
Often people put global constants into singleton objects - or as drawnonward noted, you can make them accessible via a class method of some class.
人们经常将全局常量放入单例对象中——或者正如前面提到的,你可以通过某个类的类方法来访问它们。
回答by Jared Pochtar
Use the AppController to make the colors accessible globally, rather than a static variable. That way it makes sense from an architecture standpoint, and also if you wanted to hypothetically change color schemes, even while running, this would just be a method or two on the AppController
使用 AppController 使颜色可全局访问,而不是静态变量。这样从架构的角度来看是有意义的,而且如果您想假设更改配色方案,即使在运行时,这也只是 AppController 上的一两个方法

