简体   繁体   English

Objective C定义UIColor常量

[英]Objective C defining UIColor constants

I have a iPhone application with a few custom-defined colors for my theme. 我有一个iPhone应用程序,我的主题有一些自定义颜色。 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). 由于这些颜色将为我的UI修复,我想定义要包含的类中的颜色(Constants.h和Constants.m)。 How do I do that? 我怎么做? (Simply defining them does not work because UIColors are mutable, and would cause errors - Initalizer not constant). (简单地定义它们不起作用,因为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! 谢谢!

A UIColor is not mutable. UIColor不可变。 I usually do this with colors, fonts and images. 我通常用颜色,字体和图像来做这件事。 You could easily modify it to use singletons or have a static initializer. 您可以轻松地修改它以使用单例或具有静态初始化程序。

@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

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”的任何位置,所以它永远不会得到编译,直到你实际使用它。

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

If you're looking for a quick and dirty one without extensions go with clang : 如果你正在寻找一个没有扩展的快速而肮脏的人,请使用clang

#define kGreenColor colorWithRed:(0/255.0) green:(213/255.0) blue:(90/255.0) alpha:1.0

- (void)doSomething
{
   _label.textColor = [UIColor kGreenColor];

}

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. 这有点像黑客,但你不需要在实现中重新定义颜色,你可以在没有方法调用的情况下访问颜色。

通常人们将全局常量放入单个对象中 - 或者如前所述,您可以通过某个类的类方法使它们可访问。

Use the AppController to make the colors accessible globally, rather than a static variable. 使用AppController使颜色可以全局访问,而不是静态变量。 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上的一个或两个方法

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

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