Skip to content

Instantly share code, notes, and snippets.

@0xLeif
Created October 28, 2019 16:34
Show Gist options
  • Save 0xLeif/3ba389fd0ce5426c392bcb24abb647ed to your computer and use it in GitHub Desktop.
Save 0xLeif/3ba389fd0ce5426c392bcb24abb647ed to your computer and use it in GitHub Desktop.
UIBuildable
import UIKit
postfix operator ...
protocol UIConfigurable {
func configure<T>(_ configure: (inout T) -> Void) -> T
static func ...<T>(_ lhs: Self, _ block: (inout T) -> Void) -> T
}
extension UIView: UIConfigurable {
static func ... <T>(lhs: UIView, block: (inout T) -> Void) -> T {
return lhs.configure(block)
}
func configure<T>(_ configure: (inout T) -> Void) -> T {
var buildView = self as! T
configure(&buildView)
return buildView
}
}
let backgroundView: UIView = UIView() ... {
$0.backgroundColor = .blue
}
let label: UILabel = UILabel() ... {
$0.text = "Hello World"
}
let label2: UILabel = UILabel().configure() {
$0.text = "Hello World"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment