有没有办法在文件中存储自定义颜色并从iOS中的代码访问它们? [英] Is there a way to store custom colors in a file and access them from code in iOS?
问题描述
我是iOS新手,但我有一些Android编程经验。我记得在Android中你可以在你可以在代码中使用的 colors.xml 文件中定义一组自定义颜色,因此,每次它都不需要以编程方式定义新颜色必须在某处应用。
I am an iOS newbie but I have some experience programming for Android. I recall that in Android you could define a set of custom colors in a colors.xml file that you could use in your code, and therefore, there was no need to define a new color programmatically each time it had to be applied somewhere.
我的问题是:iOS中是否存在类似的机制?我在整个互联网上搜索了一种在某个文件中创建自定义颜色的方法,并在我的iOS应用程序中以编程方式使用它们但却一无所获。
My question is: does a similar mechanism exist in iOS? I have searched throughout the Internet for a way to create custom colors once in some file and use them programmatically in my iOS app but have found nothing.
提前致谢。
推荐答案
正如vokilam建议的那样,它是总是很好用 UIColor
创建类别,并通过类别中的类方法添加自己的自定义颜色。在单个班级中有一个颜色轨道非常方便和容易。类似于:
As vokilam suggested, it is always good to create a Category using UIColor
and adding your own custom colors through class methods inside the category. It is quite handy and easy to have a track of colors inside a single class. Something like:
在Xcode中,文件 - >新建 - >新建--->文件... Cocoa Touch - > Objective-C类别 - >下一步 - >在类别列表中选择UIColor - >为类别命名 - >下一步 - >创建
In Xcode, File -> New --> New ---> File... Cocoa Touch -> Objective-C Category -> Next -> Select UIColor in "Category on" list --> Give a name for the category --> Next --> Create
这将为您提供.h和.m文件。 ..在你的.h文件中,添加一个自定义颜色方法,如
This gives you .h and .m file... In your .h file, add a custom color method like
+(UIColor *)customLightGreenColor;
在.m文件中,您可以实现类似的方法(您可以设置不同的选项)你在这里的颜色):
And in the .m file, you can implement the method something like (You have different options to set your colors here):
+(UIColor *)customlightGreenColor
{
UIColor *lightGreenColor = [UIColor colorWithRed:0.5 green:0.87 blue:0.0 alpha:1.0];
return lightGreenColor;
}
同样,您可以在类别中添加任意数量的颜色并在其中使用你的申请。希望这能让你有所了解......
Likewise, you can add any number of colors in your category and use it in your application. Hope this gives you a heads up...
这篇关于有没有办法在文件中存储自定义颜色并从iOS中的代码访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!