Skip to content

Instantly share code, notes, and snippets.

@sebskuse
Created December 15, 2017 09:30
Show Gist options
  • Save sebskuse/58e02c56b37a2958e81ff0a44dcca8d9 to your computer and use it in GitHub Desktop.
Save sebskuse/58e02c56b37a2958e81ff0a44dcca8d9 to your computer and use it in GitHub Desktop.
Allows combining multiple view styles in Swift
struct Style<View: UIView> {
let styles: [(View) -> Void]
init(_ style: @escaping (View) -> Void) {
self.styles = [style]
}
init(_ styles: [(View) -> Void]) {
self.styles = styles
}
func apply(to view: View) {
styles.forEach { $0(view) }
}
}
func +<View>(left: Style<View>, right: Style<View>) -> Style<View> {
var styles = left.styles
styles.append(contentsOf: right.styles)
return Style(styles)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment