---
title: "Creating a theme helper"
slug: creating-a-theme-helper
section: tech
date: 2016-02-21T22:43:00.000Z
canonical: https://roland.leth.ro/blog/tech/creating-a-theme-helper
---

The standard approach for this would be something like this:

```swift
struct Theme {
  enum Color {
    case title
    case subtitle
    // [...]
    var color: UIColor {
      switch self {
      case .title: return UIColor.red
      case .subtitle: return UIColor.blue
      ...
      }
    }
  }
  
  enum Font {
  }
}
```

And using it throughout the app would look like this:

```swift
func customLabel(text: String, color uiColor: Theme.Color, font uiFont: Theme.Font) {
  [...]
  label.textColor = color.uiColor
  label.font = font.uiFont
  [...]
}

let myLabel = customLabel(
  text: "Pretty label", 
  textColor: .title, 
  textFont: .title
)
myLabel.textColor = Theme.Color.subtitle.color
myLabel.font = Theme.Font.subtitle.font
```

Another approach would be with nested `structs` and `static` constants for avoiding the `color` / `font` computed property (at the cost of slight, but unnecessary extra memory usage):

```swift
struct Theme {
  struct Color {
    static let title = UIColor.red
    static let subtitle = UIColor.blue
    ...
  }
}
```

I find this approach pretty neat, but I'm not really sold vs simple extensions:

```swift
extension UIColor {
  static var title: UIColor { return UIColor.red }
  static var subtitle: UIColor { return UIColor.blue }
  ...
}
extension UIFont {
  ...
}
```

And the usage would be the one we're all already familiar with, which involves a lot less typing, too:

```swift
func customLabel(text: String, color: UIColor, font: UIFont) {
  [...]
  label.textColor = color
  label.font = font
  [...]
}

let myLabel = customLabel(
  text: "Pretty label", 
  textColor: .titleColor, // This is neatly inferred.
  textFont: .titleFont
)
myLabel.textColor = .subtitle
myLabel.font = .subtitle
```
