Alacrity
Alacrity 是一个库,为 UIView 及其子类提供流畅的接口。
示例
编写 UI 代码时,我们通常会编写如下代码
class YourCustomView: UIView {
let aView: UIView = UIView()
let aLabel: UILabel = UILabel()
func setUpUI() {
// Customize aView
aView.backgroundColor = UIColor.red
aView.layer.cornerRadius = 5.0
aView.clipToBounds = true
// Customize aLabel
aLabel.text = "Your text"
aLabel.font = UIFont.boldSystemFont(ofSize: 19.0)
aLabel.textAlignment = .center
aLabel.backgroundColor = .orange
}
}
或
class YourCustomView: UIView {
let aView: UIView = {
let view: UIView = UIView()
view.backgroundColor = UIColor.red
view.layer.cornerRadius = 5.0
view.clipToBounds = true
return view
}()
let aLabel: UILabel = {
let label: UILabel = UILabel()
label.text = "Your text"
label.font = UIFont.boldSystemFont(ofSize: 19.0)
label.textAlignment = .center
label.backgroundColor = .orange
return label
}()
}
使用 Alacrity,我们可以通过利用其流畅接口,编写更简洁的代码,而不需要典型的编程 UI 代码的样板代码
class YourCustomView: UIView {
let aView: UIView = UIView().avd
.backgroundColor(UIColor.red)
.cornerRadius(5.0)
.view
let aLabel: UILabel = UILabel().acy
.text("Your text")
.font(UIFont.boldSystemFont(ofSize: 19.0))
.textAlignment(.center)
.backgroundColor(.orange)
.view
}
需求
Alacrity 需要 iOS 10.0 或更高版本和 Swift 3.x
安装
- 将以下内容添加到您的 Podfile
pod 'Alacrity'
- 使用框架集成依赖项:将
use_frameworks!
添加到 Podfile。 - 运行
pod install
。
作者
Julio Alorro
许可证
Alacrity根据MIT许可证提供。更多详细信息请参阅LICENSE文件。