在项目中全局声明UIColor [英] Globally declare UIColor in project

查看:180
本文介绍了在项目中全局声明UIColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

目标C定义UIColor常量

我想要我的应用程序中使用少量颜色。而不是在每个实例或每个viewController中创建一个UIColor,我可以通过它在整个应用程序中使用它。

I'd like to use few colours throughout my app. Instead of creating a UIColor at every instance or in every viewController is there a way by which i can use it throughout the app.

或使用ColourConstants是明智的。 h头文件,其中我#define我要使用的每种颜色

or is it wise to use a ColourConstants.h header file where i #define each colour i want to use

#define SCARLET [UIColor colorWithRed:207.0/255.0 green:47.0/255.0 blue:40.0/255.0 alpha:1];

提前感谢!

推荐答案

我会在UIColor上使用一个类别。例如:

I would use a category on UIColor. For example:

// In UIColor+ScarletColor.h

@interface UIColor (ScarletColor)

+ (UIColor*)scarletColor;

@end


// In UIColor+ScarletColor.m

@implementation UIColor (ScarletColor)

+ (UIColor*)scarletColor {
    return [UIColor colorWithRed:207.0/255.0 green:47.0/255.0 blue:40.0/255.0 alpha:1];
}

@end

当你想使用时您只需要执行此操作的颜色:

And when you want to use the color you only have to do this:

#import "UIColor+ScarletColor.h"

....

UIColor *scarlet = [UIColor scarletColor];

希望有所帮助!!

这篇关于在项目中全局声明UIColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆