Objective C 定义 UIColor 常量

Objective C defining UIColor constants(Objective C 定义 UIColor 常量)

本文介绍了Objective C 定义 UIColor 常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 iPhone 应用程序,为我的主题提供了一些自定义颜色.由于这些颜色将为我的 UI 固定,因此我想在要包含的类中定义颜色(Constants.h 和 Constants.m).我怎么做?(简单地定义它们是行不通的,因为 UIColors 是可变的,并且会导致错误 - Initalizer 不是常量).

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).

/* Constants.h */
extern UIColor *test;

/* Constants.m */
UIColor *test = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];

谢谢!

推荐答案

UIColor 是不可变的.我通常用颜色、字体和图像来做这件事.您可以轻松修改它以使用单例或使用静态初始化程序.

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.

@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

这篇关于Objective C 定义 UIColor 常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Objective C 定义 UIColor 常量

基础教程推荐