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].
Like Android XML, iOS also used XML file for Designing the UI part call Storyboard or nib file.
A color value defined in XML. The color is specified with an RGB value and alpha channel. You can use a color resource any place that accepts a hexadecimal color value. You can also use a color resource when a drawable resource is expected in XML (for example, android:drawable="@color/green" ).
Better later than never...
For Swift:
Create a new class: MyColors.swift that is an extension of UIColor class:
extension UIColor{      static func color1() -> UIColor{          return Utils.UIColorFromRGB(0x333333)     }      static func color2() -> UIColor{          return Utils.UIColorFromRGB(0xffffff)     } } Having in another class (maybe Utils.swift) this func:
class func UIColorFromRGB(rgbValue: UInt) -> UIColor {     return UIColor(         red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,         green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,         blue: CGFloat(rgbValue & 0x0000FF) / 255.0,         alpha: CGFloat(1.0)     ) } You can use it like this (note the brackets after UIColor):
tableView.backgroundColor = UIColor.color1() Edit: Changed the functions to static so that the colors can be used like the normal iOS colors without creating an instance of 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]; If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With