clocksmith clocksmith - 4 months ago 8
iOS Question

What is the iOS equivalent of Android's 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>


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

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

Answer

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];