Skip to content

Instantly share code, notes, and snippets.

@aainaj
Last active April 3, 2020 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aainaj/4cfccf76282020b44900a45d6fbea144 to your computer and use it in GitHub Desktop.
Save aainaj/4cfccf76282020b44900a45d6fbea144 to your computer and use it in GitHub Desktop.
UIView Builder Pattern
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