Created
October 28, 2019 16:34
-
-
Save 0xLeif/3ba389fd0ce5426c392bcb24abb647ed to your computer and use it in GitHub Desktop.
UIBuildable
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
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