---
title: "#270: Implementing Theming with CostumeKit 🎩"
url: https://littlebitesofcocoa.com/270-implementing-theming-with-costumekit
author: Jake Marsh
date: 2016-09-29
published: 2016-09-29
topics: ["Libraries","UIKit"]
description: "We've looked at protocols in Swift in Bite #232, but we haven't really seen a ton of \"real world\" examples. Today we'll check out a library called CostumeKit."
---

# #270: Implementing Theming with CostumeKit 🎩

By Jake Marsh, September 29, 2016. HTML: https://littlebitesofcocoa.com/270-implementing-theming-with-costumekit

We've looked at **protocols** in **Swift** in [Bite #232](https://littlebitesofcocoa.com/232), but we haven't really seen a ton of "real world" examples. Today we'll check out a library called [CostumeKit](https://github.com/jakemarsh/CostumeKit). It's a set of base protocols that can assist us in visually styling or "themeing" our **apps**. Along the way we'll use some protocols "for real". Let's jump in.

<i>Author's Note: Full disclosure, [CostumeKit](https://github.com/jakemarsh/CostumeKit) is written by me. I'm a huge fan of conventions, also it's a nice basic example of Protocols. I use it in all my apps.</i>

To try this out, we'll be working on an imaginary <i>Little Bites of Cocoa</i> **app**.

We'll start by adding [CostumeKit](https://github.com/jakemarsh/CostumeKit) to our **project** with [Carthage](https://littlebitesofcocoa.com/11):

``` ruby
github "jakemarsh/CostumeKit"
```

Next, we'll start implementing our **types**. First up is the **colors** we'd like to use.

We'll make an **enum** and paste in all the **colors** we want to use, giving each a friendly name:

``` swift
public enum LittleBitesColors : Color, ColorPalette {
  case beige = "FEFAF1"
  case lightBrown = "AB9372"
  case brown = "8C5637"
  case lightGray = "ECECEC"
  case darkGray = "6A6A6A"
  case sponsorRowGray = "F5F5F5"
}
```

Sharped-eyed-readers will actually recognize this technique from [Bite #255](https://www.littlebitesofcocoa.com/255-creating-a-colorconvertible-protocol) on <i>Creating a ColorConvertible Protocol</i>.

Moving along, <i>LBOC</i> uses the [Source Sans Pro](https://fonts.google.com/specimen/Source+Sans+Pro) **font**. Let's implement [CostumeKit's](https://github.com/jakemarsh/CostumeKit) **Font protocol** for this. We'll [add the font files to our app](https://www.littlebitesofcocoa.com/15) then implement the **protocol**:

``` swift
public struct LittleBitesFont : Font {
  public init(size: FontSize = .textStyle(.body)) {
    self.size = size
  }

  // Font

  public var size: FontSize

  // FontConvertible

  public var FontValue: UIFont {
    return UIFont(name: "SourceSansPro", size: pointSize)!
  }
}
```

We're almost there, next we need to create the actual **Costume** our **app** will wear. For this we'll implement one more **protocol**. This **protocol** has no requirements. It serves as more of a <i>convention</i> really. 

``` swift
open class LittleBitesCostume : Costume {
  let spacing = CGFloat(8)

  public func wearRootBackground(_ view: UIView) {
    view.backgroundColor = Color.white.colorValue
  }

  public func wearHeadline(_ label: UILabel) {
    label.font = LittleBitesFont(size: .textStyle(.title1)).fontValue
    label.textColor = contentTextColor().colorValue
  }
```

``` swift
  public func contentTextColor() -> Color {
    return LittleBitesColors.darkGray
  }

  public var name: String { return "Default" }
  public var description: String { return "The default costume." }

  public init() { }
}
```

For good measure, we'll define a **night-mode costume** as well:

``` swift
open class LittleBitesNightCostume : LittleBitesCostume {
  public override func contentTextColor() -> Color {
    return Color.white
  }

  override public var name: String { return "Night Mode" }
  override public var description: String { return "For reading in the dark." }
}
```

That's where [CostumeKit](https://github.com/jakemarsh/CostumeKit) stops. We get a super-tiny bit of functionality "for free" (fonts, color parsing), but the idea is that we're merely following a set of <i>conventions</i> to help guide us through writing this code.

Let's finish up by looking at how we might actually use _costumes_ in our **app**. Everything from this point on would live inside our app's code (and isn't part of [CostumeKit](https://github.com/jakemarsh/CostumeKit)).

We'll make a **Wardrobe** type and static instance to manage all our _costumes_:

``` swift
import CostumeKit

public class Wardrobe {
  private(set) var current: LittleBitesCostume
  
  init(initialCostume: LittleBitesCostume) {
    current = initialCostume
  }
  
  public func change(costume: LittleBitesCostume, animated: Bool = true) {
    // TODO: In the future, we'll animate this change using UIView transitions and RxSwift.

    current = costume
  }
}

public let wardrobe = Wardrobe(initialCostume: LittleBitesCostume())
public var costume: LittleBitesCostume { return wardrobe.current }
```

Now in our regular **UIKit** code, we can use our **current costume**:

``` swift
let headlineLabel = UILabel()
headlineLabel.text = "Hello World".
costume.wearHeadline(headlineLabel)
```

Learn more about [CostumeKit](https://github.com/jakemarsh/CostumeKit) at [git.io/costumekit](https://git.io/costumekit)

<p></p>
