Xcode 8 Color Palettes

Become a Subscriber

Usually each app holds a palette of brand colors, which are used across various areas of the application. Usually this kind of data is stored in a constant file with let orangeColor = UIColor.orange, but here is a more readable and usable way to construct the color palette with freshly introduced Color Literal in Xcode 8.

In swift, constantly reusable data such as color palettes can be conveniently stored in struct constructs. This approach is better as it avoids using global constants and dot-notation tends to be more readable code.

struct UniColors {
    struct Orange {
        static let lighten1 = UIColor(hex: "#F39608") 
        static let darken1 = UIColor(hex: "#CD652F") 

That convenient initializer of UIColor type is basically processing HEX color code in String to RGB channels and init a UIColor object based on that data. There are plenty of examples in the internet of how you can do that. I like to use this lib. It’s pretty powerful and lightweight.

With this pattern I can use app’s brand color like this:

label.textColor = UniColors.Orange.lighten

And with Color Literal I can use color picker in the comment section to see a preview of the color in other files instead of opening this file to look at hex code:

Xcode Color Literals Preview

To set a Color Literal like this, you can simply drag and drop the color you want from the Color Picker. To open that tool, go to Edit > Format > Show Colors.

Here is a preview of how how to create a color palette for Interface Builder to make it handy to use those colors, when you’re constructing UI in Storyboard or Nib file.