OhSwifter
一个便捷的 Swift 扩展和初始化器
Request
- iOS 11.0+
- Xcode 10.0+
- Swift 5.0+
安装
通过 Cocoapods 使用 OhSwifter,只需将以下行添加到您的 Podfile 中即可
pod 'OhSwifter'
为什么使用 OhSwifter?
1. UILabel
之前
private let label: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
label.textColor = .gray
label.textAlignment = .left
label.text = "Label Wording"
label.numberOfLines = 0
return label
}()
- 输入
label
七次。😱 let label = UILabel()
和return label
看起来有点多余。
对我来说太啰嗦也太长了。
之后
private let ohLabel = UILabel().oh
.style(font: UIFont.systemFont(ofSize: 12, weight: .medium), color: .gray)
.textAlignment(.left)
.text("Label Wording")
.numberOfLines(0)
.done()
- 首先,定义属性类型的代码更少。例如:
label: UILabel
。 - 其次,使用
oh
容器进行命名空间,可以区分原始函数或属性,并减少智能提示类型。
注意:
.dnoe()
是必须的。虽然我认为这有点多余,但目前还没有更好的想法。
2. UIButton
之前
private let button: UIButton = {
let button = UIButton()
button.setTitle("Button Wording", for: .normal)
button.setTitleColor(.black, for: .normal)
button.setTitleColor(.gray, for: .highlighted)
button.layer.cornerRadius = 12
button.layer.borderColor = UIColor.darkGray.cgColor
button.layer.borderWidth = 1
return button
}()
- 输入
button
八次。😱 let button = UIButton()
和return button
和 UILabel 一样多余。- 重复输入了两次
.layer
。
之后
private let ohButton = UIButton().oh
.title("Button Wording", for: .normal)
.titleColor(.black, for: .normal)
.titleColor(.gray, for: .normal)
.cornerRadius(12)
.border(color: .darkGray, width: 1)
.done()
- 不需要输入任何
button
。 - 不需要输入任何层。
- 将边框颜色和宽度合并到一个函数中调用。
3. 自定义
如果初始化 oh
后找不到样式设置,请提交一个问题。或者,您可以使用 createConfigurator。
像这样
.createConfigurator { (button) in
// ...
}
许可证
OhSwifter遵从MIT许可证发布。有关详细信息,请参阅LICENSE。