What is the iOS equivalent of Android's colors.xml
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];
As of Xcode 9 you can define colors in Asset Catalogs:
Create or use an existing Asset Catalog and click on the plus button on the bottom left of the editor and select "New Color Set":
You will see a new color called, "Color."
Click on it, then click to show the attributes inspector:
Here you can define your color and a lot of other options.
Double click on the name to change the name of the color.
Design tools like Sketch and Zeplin will allow you to export colors from your design strait into color assets for Xcode:
https://blog.zeplin.io/asset-catalog-colors-on-xcode-9-c4fdccc0381a