什么是iOS的等同于Android的colors.xml [英] What is the iOS equivalent of Android's colors.xml

查看:134
本文介绍了什么是iOS的等同于Android的colors.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android中有一个XML文件 res / values / colors.xml ,可以让你整理应用中使用的所有颜色。像这样:

In android there is an XML file as res/values/colors.xml that lets you organize all of your colors used in your app. Like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="red">#e60012</color>
  <color name="blue">#33b5e5</color>
  ...
</resources>

在iOS中有这样的东西吗?如果没有,组织整个应用程序使用的颜色的最佳方法是什么?

Is there something like this in iOS? If not, what is the best way to organize colors that are used throughout the app?

我希望最终能够替换像这样的东西[UIColor greenColor] with [MyColor greenColor]

I would like to ultimately be able to replace things like [UIColor greenColor] with [MyColor greenColor].

推荐答案

我没有遇到像这样的默认文件。您可以创建自己的自定义.plist文件,该文件包含值,并在应用启动时加载。另一种选择是为 UIColor 创建一个类别,它有一堆类方法返回你想要的颜色。

I have not come across a default file like this. You could create your own custom .plist file which holds the values and you load that when the app starts. Another option is to create a Category for UIColor which has a bunch of class methods returning the colors you want.

您可以创建如下所示的内容:

You could create something that looks like this:

UIColor + CustomColors.h

@interface UIColor (CustomColors)

    + (UIColor *)customColor1;
    + (UIColor *)customColor2;
    ...

@end

UIColor + CustomColors.m

#import "UIColor+CustomColors.h"

@implementation UIColor (CustomColors)

    + (UIColor *)customColor1 {
        return [UIColor colorWithRed:1.0f green:0.5f blue:0.5f alpha:1.0f];
    }
    + (UIColor *)customColor2 {
        return [UIColor colorWithRed:1.0f green:0.5f blue:1.0f alpha:1.0f];
    }
    ...

@end

然后在你设置背景的地方你可以有这样的东西:

Then where you set the background you could have something like this:

ViewController.m

#import "UIColor+CustomColors.h"

...

view.backgroundColor = [UIColor customColor1];

这篇关于什么是iOS的等同于Android的colors.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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