Skip to content

Instantly share code, notes, and snippets.

@ktanaka117
Last active October 21, 2017 07:36
Show Gist options
  • Save ktanaka117/2962ea16ac43432e1fb0527dce26633b to your computer and use it in GitHub Desktop.
Save ktanaka117/2962ea16ac43432e1fb0527dce26633b to your computer and use it in GitHub Desktop.
import UIKit
public struct Maker<Product> {
let product: Product
}
extension Maker where Product: UILabel {
public func set(textColor color: UIColor) -> Maker {
self.product.textColor = color
return self
}
public func set(numberOfLines number: Int) -> Maker {
self.product.numberOfLines = number
return self
}
public func set(textAlignment alignment: NSTextAlignment) -> Maker {
self.product.textAlignment = alignment
return self
}
public func set(lineBreakMode mode: NSLineBreakMode) -> Maker {
self.product.lineBreakMode = mode
return self
}
public func set(font: UIFont) -> Maker {
self.product.font = font
return self
}
}
extension UILabel {
public static func make(frame: CGRect = .zero, by making: (Maker<UILabel>) -> Maker<UILabel>) -> UILabel {
let product = UILabel(frame: frame)
let maker = Maker<UILabel>(product: product)
let resultMaker = making(maker)
let resultProduct = resultMaker.product
return resultProduct
}
}
let label = UILabel.make(frame: .zero) { $0
.set(textColor: .red)
.set(numberOfLines: 0)
.set(textAlignment: .center)
.set(lineBreakMode: .byWordWrapping)
.set(font: .systemFont(ofSize: 20))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment