Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raonivaladares/9ec7baa3895fdb357d228f06057b1d2e to your computer and use it in GitHub Desktop.
Save raonivaladares/9ec7baa3895fdb357d228f06057b1d2e to your computer and use it in GitHub Desktop.
import UIKit
final class UIButtonFactory {
private let button: UIButton
enum Style {
case normal
case especial
}
// MARK: - Inits
init(title: String, style: Style = .normal) {
button = UIButton()
button.setTitle(title, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
switch style {
case .normal: normalStyle()
case .especial: especialStyle()
}
}
// MARK: - Public methods
func addTarget(_ target: Any?, action: Selector, for event: UIControlEvents) -> Self {
button.addTarget(target, action: action, for: event)
return self
}
func build() -> UIButton {
return button
}
// MARK: - Private methods
private func especialStyle() {
button.cornerRadius = 10
button.backgroundColor = .green
button.setTitleColor(.red, for: .normal)
button.setTitleColor(.white, for: .highlighted)
}
private func normalStyle() {
button.backgroundColor = .blue
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.yellow, for: .highlighted)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment