Latest 1.0.1
Homepage https://github.com/regexident/Gestalt
License MPL-2.0
Platforms ios 9.0
Authors

jumbotron

Gestalt is an unintrusive and light-weight framework for application theming with support for animated theme switching.

screencast

Usage

Let’s say you want to theme a view controller with a single label:

import Gestalt

struct Theme: ThemeProtocol {
    let font = UIFont.preferredFont(forTextStyle: .headline)
    let color: UIColor
    let backgroundColor: UIColor

    static let light = Theme(
        color: UIColor.black
        backgroundColor: UIColor.white
    )

    static let dark = Theme(
        color: UIColor.white
        backgroundColor: UIColor.black
    )
}

// In `AppDelegate.application(_:didFinishLaunchingWithOptions:)`
// assign a default theme (or user's choice from user defaults):
ThemeManager.default.theme = Theme.light

class ViewController: UIViewController {
    @IBOutlet var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        ThemeManager.default.apply(theme: Theme.self, to: self) { themeable, theme in
            themeable.view.backgroundColor = theme.backgroundColor
            themeable.label.textColor = theme.color
            themeable.label.font = theme.font
        }
    }
}

A call to ThemeManager.apply(theme:to:animated:closure:) registers the closure passed to it on the given ThemeManager for future theme changes and then calls it once immediately. If you want the initial call to be animated, make sure to pass …, animated: true, … to it (the default is false).

To change the current theme (even while the app is running) simply assign a different theme to your given ThemeManager in use:

ThemeManager.default.theme = Theme.dark

This will cause all previously registered closures on the given ThemeManager to be called again.

Note:

  1. It is generally sufficient to use ThemeManager.default. It is however possible to create dedicated ThemeManagers via let manager = ThemeManager().
  2. The value passed to animated overrides the ThemeManager‘s default setting
    of var animated: Bool for the initial call of the closure.

Important:

  1. Within closure any access on themeable should only achieved through
    the closure’s $0 argument, not directly, to avoid retain cycles.
  2. The body of closure should be idempotent
    to avoid unwanted side-effects on repeated calls.

Installation

The recommended way to add Gestalt to your project is via Carthage:

github 'regexident/Gestalt'

or via Cocoapods:

pod 'Gestalt'

License

Gestalt is available under the MPL-2.0 license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Gestalt",
    "version": "1.0.1",
    "summary": "An unintrusive & light-weight app-theming framework.",
    "description": "An unintrusive & light-weight framework for app-themingnwith support for animated theme switching.",
    "homepage": "https://github.com/regexident/Gestalt",
    "license": {
        "type": "MPL-2.0",
        "file": "LICENSE"
    },
    "authors": {
        "Vincent Esche": "regexident@gmail.com"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/regexident/Gestalt.git",
        "tag": "1.0.1"
    },
    "source_files": "Gestalt/**/*.{h,m,swift}",
    "pushed_with_swift_version": "3.2"
}

Pin It on Pinterest

Share This