Latest 1.0.2
Homepage https://github.com/iwheelbuy/Decorator
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform

pod 'Decorator', '1.0.2'

Decorations

Decoration is a simple generic closure which receives an object to decorate

typealias Decoration<T> = (T) -> Void

There are plenty of ways to produce decorations

let decorationNormal = { (label: UILabel) in
    // apply any required changes to the label
}

struct Style {

    static var decorationSelected: Decoration<UILabel> {
        return { (view: UILabel) in
            // apply any required changes to the label
        }
    }
}

Decorations can be composed using simple infix operator +

let decoration = decorationNormal + Style.decorationSelected + { (label: UILabel) -> Void in
    // apply any required changes to the label
}

How to use

It is a typical way of setting up a view and its properties programmatically, isn’t it?

final class ViewController: UIViewController {

    func setup() {
        label.textAlignment = NSTextAlignment.center
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14)
        label.backgroundColor = UIColor.white
    }

    func didSelect() {
        label.textColor = UIColor.white
        label.backgroundColor = UIColor.orange
    }

    func didDeselect() {
        label.textColor = UIColor.darkGray
        label.backgroundColor = UIColor.white
    }
}

Why not moving all the "styling" away to some other place and just apply styles to views when necessary.

final class ViewController: UIViewController {

    func setup() {
        label.decorator.apply(decorationNormal)
    }

    func didSelect() {
        label.decorator.apply(decorationSelected)
    }

    func didDeselect() {
        label.decorator.apply(decorationNormal)
    }
}

Would like to declare all possible styles at once and then just apply them by passing a name of a style?

final class ViewController: UIViewController {

    func setup() {
        label.decorator["Default"] = decorationNormal
        label.decorator["Selected"] = decorationSelected
        label.decorator.style = "Default"
    }

    func didSelect() {
        label.decorator.style = "Selected"
    }

    func didDeselect() {
        label.decorator.style = "Default"
    }
}

Author

iwheelbuy, [email protected]

License

Decorator is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Decorator",
    "version": "1.0.2",
    "summary": "Decoration = (T) -> Void",
    "description": "Generic decorator for UIView or any other class",
    "homepage": "https://github.com/iwheelbuy/Decorator",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "iwheelbuy": "[email protected]"
    },
    "source": {
        "git": "https://github.com/iwheelbuy/Decorator.git",
        "tag": "1.0.2"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Decorator/Classes/**/*",
    "pushed_with_swift_version": "3.1"
}

Pin It on Pinterest

Share This