Created
March 20, 2017 19:15
-
-
Save Kalvin126/ace8f6a35c4b453515e13530d18a6640 to your computer and use it in GitHub Desktop.
Reusable Styler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
class Styler<ViewType: UIView> { | |
private let styler: (ViewType) -> Void | |
init(_ styler: @escaping (ViewType) -> Void) { | |
self.styler = styler | |
} | |
func apply(on view: ViewType) { | |
styler(view) | |
} | |
} | |
extension Styler where ViewType: UIView { | |
static var blueBackground: Styler { | |
return Styler { | |
$0.backgroundColor = .blue | |
} | |
} | |
} | |
extension Styler where ViewType: UIButton { | |
static var defaultText: Styler { | |
return Styler { | |
// Call other "parent" styles | |
Styler.blueBackground.apply(on: $0) | |
$0.setTitle("defaultText", for: .normal) | |
} | |
} | |
static var blue: Styler { | |
return Styler { | |
$0.backgroundColor = .blue | |
} | |
} | |
} | |
extension UIButton { | |
// TODO: How to generalize this accross all UIViews? | |
func apply(_ styler: Styler<UIButton>) { | |
styler.apply(on: self) | |
} | |
} | |
let button = UIButton(type: .custom) | |
button.apply(.defaultText) | |
button.titleLabel?.text | |
button.backgroundColor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment