Last active
April 3, 2020 07:37
-
-
Save aainaj/4cfccf76282020b44900a45d6fbea144 to your computer and use it in GitHub Desktop.
UIView Builder Pattern
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
extension UIView { | |
@inline(__always) static func build<T>(applyAttributes: ((T) -> Void)? = nil) -> T where T: UIView { | |
let uiComponent = T(frame: .zero) | |
uiComponent.translatesAutoresizingMaskIntoConstraints = false | |
applyAttributes?(uiComponent) | |
return uiComponent | |
} | |
} | |
let label: UILabel = { | |
return UILabel.build { label in | |
label.text = "Hello wold!" | |
label.font = UIFont.systemFont(ofSize: 12) | |
} | |
} | |
let view: UIView = { | |
return UIView.build() | |
} | |
let button: UIButton = { | |
return UIButton.build { | |
$0.setTitle("Button", for: .normal) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment