Alacrity is a library that brings a fluent interface for UIView and its subclasses.
When writing UI code programmatically we would typically write code such as this:
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
}
}
or
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
}()
}
With Alacrity we can write something more succinct, without the boilerplate of typical programmatic UI code by taking advantage of its fluent interface
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 requires iOS 10.0 or higher and Swift 3.x
- Add the following to your Podfile:
pod 'Alacrity'
- Integrate your dependencies using frameworks: add
use_frameworks!
to your Podfile. - Run
pod install
.
Julio Alorro
Alacrity is available under the MIT license. See the LICENSE file for more info.