使用如下:注:我是配合snpkit布局使用的,所以不写frame属性
实例一个UIButton,
1 private let sureButton = UIButton() 2 .fontSize(UIFont.systemFontSize()) 3 .title("确定", forState: .Normal) 4 .titleColor(.whiteColor(), forState: .Normal) 5 .titleColor(.smallGreenColor(), forState: .Highlighted) 6 .backgroundColor(.smallGrayColor(), forState: .Normal) 7 .backgroundColor(.whiteColor(), forState: .Highlighted) 8 .borderColor(.smallGrayColor()) 9 .borderWidth(1) 10 .cornerRadius(8) 11 .userInteractionEnabled(false)
实例一个UILable,
private let tipTextLabel = UILabel() .fontSize(14) .text("请填写您本人的银行卡信息") .textColor(.grayColor())
附各个UI控件源码:
UIButton.h
1 // 2 // UIButton.swift 3 // DPplatform 4 // 5 // Created by 王雄皓 on 16/6/18. 6 // Copyright © 2016年 王雄皓. All rights reserved. 7 // 8 9 import Foundation 10 import UIKit 11 12 13 extension UIButton { 14 15 16 func title(title: String?, forState: UIControlState) -> UIButton { 17 18 self.setTitle(title, forState: forState) 19 return self 20 } 21 22 func titleLabel(fontSize size: CGFloat) -> UIButton { 23 24 self.titleLabel?.fontSize(size) 25 return self 26 } 27 28 func textAlignment(alignment: NSTextAlignment) -> UIButton { 29 30 self.titleLabel?.textAlignment = alignment 31 return self 32 } 33 34 func fontSize(size: CGFloat) -> UIButton { 35 36 self.titleLabel?.fontSize(size) 37 return self 38 } 39 40 func contentMode(mode: UIViewContentMode) -> UIButton { 41 42 self.contentMode = mode 43 return self 44 } 45 46 func titleColor(color: UIColor?, forState: UIControlState) -> UIButton { 47 48 self.setTitleColor(color!, forState: forState) 49 return self 50 } 51 52 53 func font(font: UIFont?) -> UIButton { 54 55 self.titleLabel!.font = font 56 return self 57 } 58 59 func cornerRadius(size: CGFloat) -> UIButton { 60 61 self.layer.cornerRadius = size 62 self.layer.masksToBounds = true 63 return self 64 } 65 66 func backgroundColor(color: UIColor?, forState: UIControlState) -> UIButton { 67 68 self.setBackgroundColor(color!, forState: forState) 69 return self 70 } 71 72 func backgroundImage(image: UIImage?, forState: UIControlState) -> UIButton { 73 74 self.setBackgroundImage(image, forState: forState) 75 return self 76 } 77 78 func contentHorizontalAlignment(alignment: UIControlContentHorizontalAlignment) -> UIButton { 79 80 self.contentHorizontalAlignment = alignment 81 return self 82 } 83 84 func borderWidth( CGFloat) -> UIButton { 85 86 self.layer.borderWidth = width 87 88 89 guard self.layer.masksToBounds else{ 90 91 self.layer.masksToBounds = true 92 return self 93 } 94 return self 95 } 96 97 func borderColor(color: UIColor?) -> UIButton { 98 99 self.layer.borderColor = color!.CGColor 100 101 guard self.layer.masksToBounds else{ 102 103 self.layer.masksToBounds = true 104 return self 105 } 106 107 return self 108 } 109 110 func selected(selected: Bool) -> UIButton { 111 112 self.selected = selected 113 return self 114 } 115 116 func image(image: UIImage?,forState: UIControlState) -> UIButton { 117 118 self.setImage(image, forState: forState) 119 return self 120 } 121 122 override func userInteractionEnabled(enable: Bool) -> UIButton { 123 124 self.userInteractionEnabled = enable 125 return self 126 } 127 128 func hidden(hide: Bool) -> UIButton { 129 130 self.hidden = hide 131 return self 132 } 133 134 override func tag(t: Int) -> UIButton { 135 136 self.tag = t 137 return self 138 } 139 }
UILabel.h
1 // 2 // UILabel.swift 3 // DPplatform 4 // 5 // Created by 王雄皓 on 16/6/18. 6 // Copyright © 2016年 王雄皓. All rights reserved. 7 // 8 9 import Foundation 10 import UIKit 11 12 13 extension UILabel { 14 15 override func backgroundColor(color: UIColor?) -> UILabel { 16 17 self.backgroundColor = color 18 return self 19 } 20 21 func text(text: String?) -> UILabel { 22 23 self.text = text 24 return self 25 } 26 27 func font(font: UIFont) -> UILabel { 28 29 self.font = font 30 return self 31 } 32 33 func textColor(color: UIColor) -> UILabel { 34 35 self.textColor = color 36 return self 37 } 38 39 func shadowColor(color: UIColor?) -> UILabel { 40 41 self.shadowColor = color 42 return self 43 } 44 45 func shadowOffset(size: CGSize) -> UILabel { 46 47 self.shadowOffset = size 48 return self 49 } 50 51 func textAlignment(type: NSTextAlignment) -> UILabel { 52 53 self.textAlignment = type 54 return self 55 } 56 57 func lineBreakMode(mode: NSLineBreakMode) -> UILabel { 58 59 self.lineBreakMode = mode 60 return self 61 } 62 63 func fontSize(size: CGFloat) -> UILabel { 64 65 self.font = .systemFontOfSize(size) 66 return self 67 } 68 69 func hidden(hid: Bool) -> UILabel { 70 71 self.hidden = hid 72 return self 73 } 74 75 func alpha(al: CGFloat) -> UILabel { 76 77 self.alpha = al 78 return self 79 } 80 81 82 override func userInteractionEnabled(enable: Bool) -> UILabel { 83 84 self.userInteractionEnabled = enable 85 return self 86 } 87 88 func number(ofLines ofLines: Int) -> UILabel { 89 90 self.numberOfLines = ofLines 91 return self 92 } 93 94 func minimumScaleFactor(factor: CGFloat) -> UILabel { 95 96 self.adjustsFontSizeToFitWidth = true 97 self.minimumScaleFactor = factor 98 return self 99 } 100 101 }