Skip to content
Marek Fořt edited this page Aug 30, 2018 · 3 revisions

Every app has usually defined a set of colors, fonts and repeating UI elements - a theme. There are dozens of ways how to define these constants and it's totally up to you how you will do it, but here is a brief description of our way.

ThemeProvider

This protocol with its extension provides .theme namespace on all supported objects. It's done in ThemeProvider .swift and you don't have to touch this code.

struct Theme<Base> { }

protocol ThemeProvider { }

extension ThemeProvider {
    static var theme: Theme<Self>.Type { return Theme<Self>.self }

    var theme: Theme<Self> { return Theme<Self>() }
}

Theme definition

To define theme just make your class conform to ThemeProvider protocol and define properties or methods in extensions like this.

extension UIColor: ThemeProvider { }

extension Theme where Base: UIColor {
    static var ackeeBlue: UIColor { return .blue }
}
extension UIButton: ThemeProvider { }

extension Theme where Base: UIButton {
    func makeRoundedBlue() {
        base.setTitleColor(UIColor.theme.ackeeBlue, for: .normal)
        base.layer.cornerRadius = 25
        base.layer.masksToBounds = true
    }
}

Theme usage

After that you can use theme this way.

titleLabel.textColor = UIColor.theme.ackeeBlue

nextButton.theme.makeRoundedBlue()

Continue to Tests ➡️

Clone this wiki locally