Functions Init(object, setup) - initializer pattern. var view = Init(UIView) {[unowned self] in $0.backgroundColor = UIColor.whiteColor(); self.view.addSubview($0) }
https://github.com/ivabra/IBSwiftToolKit/tree/6ad06dc794119d3c7fa8d72f2328239261f8034b
extension UILabel { func getBlock(block:(make:UILabel) -> Void) -> Void { let label = self block(make:label) } /// 快速创建一个UILabel class func getLabel(block:(make:UILabel) -> Void) -> UILabel { let label = UILabel() block(make:label) return label } } eventDescriptionPlaceholderLabel.getBlock(){ (make) in make.text = "Description (Optional)" make.font = UIFont(name: "Helvetica", size: 17) make.sizeToFit() make.userInteractionEnabled = false make.frame.origin = CGPointMake(0, 0) make.textColor = ColorFromCode.colorWithHexString("#BEBEBE") } eventDescriptionPlaceholderLabel = UILabel.getLabel({ (make) in make.text = "Description (Optional)" make.font = UIFont(name: "Helvetica", size: 17) make.sizeToFit() make.userInteractionEnabled = false make.frame.origin = CGPointMake(0, 0) make.textColor = ColorFromCode.colorWithHexString("#BEBEBE") })
class Tool { /// 快速创建一个UILabel class func getLabel(@noescape block:(ws:UILabel) -> Void) -> UILabel { let label = UILabel() block(ws:label) return label } class func getView(@noescape block:(ws:UIView) -> Void) -> UIView { let view = UIView() block(ws:view) return view } class func getImageView(@noescape block:(ws:UIImageView) -> Void) -> UIImageView { let view = UIImageView() block(ws:view) return view } class func getButton(@noescape block:(ws:UIButton) -> Void) -> UIButton { let view = UIButton() block(ws:view) return view } }
extension UILabel { override func getBlock(@noescape block:(ws:UILabel) -> Void) -> Void { let label = self block(ws:label) } /// 快速创建一个UILabel class func getLabel(@noescape block:(ws:UILabel) -> Void) -> UILabel { let label = UILabel() block(ws:label) return label } } extension UIButton { override func getBlock(@noescape block:(ws:UIButton) -> Void) -> Void { let button = self block(ws:button) } } extension UIImageView { override func getBlock(@noescape block:(ws:UIImageView) -> Void) -> Void { let view = self block(ws:view) } } extension UIView { func getBlock(@noescape block:(ws:UIView) -> Void) -> Void { let view = self block(ws:view) } }
请发表评论