Skip to content

Instantly share code, notes, and snippets.

@felginep
Created January 18, 2019 09:05
Show Gist options
  • Save felginep/0148b40e26b19d07e81c2e1e4f2ff3d2 to your computer and use it in GitHub Desktop.
Save felginep/0148b40e26b19d07e81c2e1e4f2ff3d2 to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
struct ViewStyle<T> {
let style: (T) -> Void
}
let filled = ViewStyle<UIButton> {
$0.setTitleColor(.white, for: .normal)
$0.backgroundColor = .red
}
let rounded = ViewStyle<UIButton> {
$0.layer.cornerRadius = 4.0
}
extension ViewStyle {
func compose(with style: ViewStyle<T>) -> ViewStyle<T> {
return ViewStyle<T> {
self.style($0)
style.style($0)
}
}
}
let roundedAndFilled = rounded.compose(with: filled)
extension ViewStyle where T: UIButton {
static var filled: ViewStyle<UIButton> {
return ViewStyle<UIButton> {
$0.setTitleColor(.white, for: .normal)
$0.backgroundColor = .red
}
}
static var rounded: ViewStyle<UIButton> {
return ViewStyle<UIButton> {
$0.layer.cornerRadius = 4.0
}
}
static var roundedAndFilled: ViewStyle<UIButton> {
return rounded.compose(with: filled)
}
}
func style<T>(_ object: T, with style: ViewStyle<T>) {
style.style(object)
}
protocol Stylable {
init()
}
extension UIView: Stylable {}
extension Stylable {
init(style: ViewStyle<Self>) {
self.init()
apply(style)
}
func apply(_ style: ViewStyle<Self>) {
style.style(self)
}
}
let button = UIButton(style: .roundedAndFilled)
button.setTitle("My Button", for: .normal)
button.sizeToFit()
button
@seanrucker
Copy link

seanrucker commented Mar 8, 2019

How could you allow composing ViewStyles for UIView subclasses with ViewStyles for UIView?

extension ViewStyle {
    static var buttonStyle: ViewStyle<UIButton> {
        return ViewStyle<UIButton> {
            $0.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        }
    }
    
    static var roundedAndFilledButton: ViewStyle<UIButton> {
        // This doesn't work because 'roundedAndFilled' returns a 'ViewStyle<UIView>' and buttonStyle is expecting 'ViewStyle<UIButton>'
        return buttonStyle.compose(with: .roundedAndFilled)
    }
}

@ConEliopoulos
Copy link

I like the idea of overloading the + operator

extension ViewStyle {

    static func +(left: ViewStyle<T>, right: ViewStyle<T>) -> ViewStyle<T> {
        return ViewStyle<T> {
            left.style($0)
            right.style($0)
            return $0
        }
    }
}

So then you can do

let button = UIButton(style: rounded + filled)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment