问题描述
我想在实用程序文件中创建一个通用方法来为我的应用程序中的标签设置颜色.我通过应用程序为所有标签设置了不同的颜色.
I want to create a common method in Utility file to set colour for labels in my application. I have different different colours for all labels through application.
如何编写常用的颜色设置方法?.
How to write common method to set colours?.
推荐答案
我建议使用类别.创建一个名为UIColor+Branding"或类似的类别.在这里,您将拥有一份您需要的所有颜色的列表.然后你可以做的就是将该类别导入到需要这些自定义颜色的文件中 import "UIColor+Branding.h"
然后你就可以调用 [UIColor myCustomColor]代码>.
I would recommend is using a category. Make a category called "UIColor+Branding" or something similar. In here, you would have a list of all the colours you need. What you can do then, is simply import that category into your files that need these custom colours import "UIColor+Branding.h"
and you will be able to call [UIColor myCustomColor]
.
UIColor+Branding.h
UIColor+Branding.h
@interface UIColor (Branding)
+ (UIColor)myCustomColor;
@end
UIColor+Branding.m
UIColor+Branding.m
#import "UIColor+Branding.h"
@implementation UIColor (Branding)
+ (UIColor)myCustomColor
{
return [UIColor colorWithRed:4/255.0 green:181/255.0 blue:13/255.0 alpha:1.0];
}
@end
在需要自定义颜色的类中:
In the class that needs the custom colours:
#import "UIColor+Branding.h"
lblTitle.textColor = [UIColor myCustomColor];
此方法可让您的应用程序中的品牌更加灵活.但是,如果您不关心这一点,那么在您的前缀中,只需导入 UIColor+Branding 头文件并在任何地方使用它,或者使用其他方法.
This method allows for more flexibility with the branding in your application. However, if you don't care for that, then in your prefix, just import the UIColor+Branding header file and use it everywhere, or use the other methods.
这篇关于通过ios中的应用程序为不同的UILabels提供不同的UIColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!