NerdzStyle library allow to easily create and apply CSS-like styles to view elements.
You will need to create a style to be able to use it later on. There are two options how you can do that.
let borderedViewStyle = UIView.style {
$0.layer.borderWidth = 1
$0.layer.borderColor = UIColor.white.cgColor
$0.layer.cornerRadius = 16
}let borderedViewStyle = Style<UIButton> {
$0.layer.borderWidth = 1
$0.layer.borderColor = UIColor.white.cgColor
$0.layer.cornerRadius = 16
}Also you can inherit from another style to create a custom hierarchy
let plainButtonStyle = UIButton.style(parent: borderedViewStyle) {
$0.setTitleColor(.green, for: .normal)
$0.layer.borderColor = UIColor.green.cgColor
$0.titleLabel?.font = UIFont.preferredFont(forTextStyle: .largeTitle)
}let plainButtonStyle = Style<UIButton>(parent: borderedViewStyle) {
$0.setTitleColor(.green, for: .normal)
$0.layer.borderColor = UIColor.green.cgColor
$0.titleLabel?.font = UIFont.preferredFont(forTextStyle: .largeTitle)
}To apply Style into some view instance you can use one of two possible ways
myButton.apply(plainButtonStyle)plainButtonStyle.apply(to: myButton)Also you can apply more than one style at once. The style priority will be increasing from left to right
myButton.apply(buttonStyle1, buttonStyle2, buttonStyle3)You can also apply same style to several views
plainButtonStyle.apply(to: button1, button2, button3)Because of a generics you will be forced to use the same generic parameters as view type you are applyying style to. If you want to apply style with another generic parameter - you can user wrapped function from Style class
myButton.apply(borderedViewStyle.wrapped())You can use CocoaPods dependency manager to install NerdzStyle.
In your Podfile spicify:
pod 'NerdzStyle', '~> 1.0'To add NerdzStyle to a Swift Package Manager based project, add:
.package(url: "https://github.com/nerdzlab/NerdzStyle")This code is distributed under the MIT license. See the LICENSE file for more info.