Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to store custom colors in a file and access them from code in iOS?

Tags:

ios

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.

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.

Thanks in advance.

like image 633
NickJaremek Avatar asked Jan 22 '26 22:01

NickJaremek


2 Answers

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:

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

This gives you .h and .m file... In your .h file, add a custom color method like

+(UIColor *)customLightGreenColor;

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...

like image 191
GenieWanted Avatar answered Jan 25 '26 11:01

GenieWanted


You can extend UIColor class with category and define and implement colors there. Like [UIColor blackColor].

In fact, you can abstract from particular color names and use method names such as primaryColor, secondaryColor, defaultTextColor, alternativeTextColor, inverseTextColor and so on.

like image 20
vokilam Avatar answered Jan 25 '26 12:01

vokilam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!