-
Notifications
You must be signed in to change notification settings - Fork 2
ViewStyle
It allows you to apply certain properties to any UIView
subclass providing the setter not only on instance member but also as UIAppearance
selector.
Within general setup view styles provides properties of [.backgroundColor, .tintColor, .borderColor, .borderWidth, .cornerRadius, .opacity, .shadow]
. You can set any of these properties to any view you choose.
let pill = ViewStyle(
.cornerRadius(10),
.borderWidth(3),
.borderColor(.red),
.opacity(0.8)
)
UILabel.appearance().viewStyle = pill
let red = ViewStyle(
.backgroundColor(.red),
.tintColor(.red),
.borderColor(.red),
.borderWidth(0.5),
.shadow(Shadow(
color: .red,
offset: UIOffset(horizontal: 0, vertical: 8),
radius: 16
))
)
let blue = ViewStyle(
.borderColor(.blue),
.borderWidth(0.5),
.shadow(.none)
)
UITextField.appearance().setViewStyle(red, for: .editing)
UITextField.appearance().setViewStyle(blue, for: .inactive)
UITextView.appearance().setViewStyle(red, for: .editing)
UITextView.appearance().setViewStyle(blue, for: .inactive)
let blue = ViewStyle(
.borderColor(.blue),
.borderWidth(0.5),
.cornerRadius(10)
)
myButton.viewStyle = blue
If you set any of [.borderColor, .borderWidth, .cornerRadius, .opacity, .shadow]
properties for particular states to UITextView
or UITextField
, the Styles will require to match properties under the hood. If you don't do so the styling becomes invalid because layer properties are not reset when state is changed. It is up to you to define the style. Below is the example which produces assertion error and second snippet is valid configuration.
let redBorder = ViewStyle(
.borderColor(.red),
.borderWidth(1.5)
)
let roundedCorners = ViewStyle(
.cornerRadius(10)
)
UITextField.appearance().setViewStyle(redBorder, for: .editing)
// It throws an assertion error
// because rounded corners is missing .borderColor & .borderWidth
UITextField.appearance().setViewStyle(roundedCorners, for: .inactive)
let redRoundedBorder = ViewStyle(
.borderColor(.red),
.borderWidth(1.5),
.cornerRadius(10)
)
let redFlatBorder = ViewStyle(
.borderColor(.red),
.borderWidth(1.5),
.cornerRadius(0)
)
UITextField.appearance().setViewStyle(redRoundedBorder, for: .editing)
UITextField.appearance().setViewStyle(redFlatBorder, for: .inactive)
You can even update the ViewStyle with any other ViewStyle. Even, you can chain the .updating
function and create awesome effects. If you have a style, where you need to update specific property, just apply .updating
to the style. We use right associativity rule in the .updating
function.
let app = ViewStyle(
.borderWidth(0.5),
.cornerRadius(10)
)
let blue = app.updating(.borderColor(.blue))
let app = ViewStyle(
.borderWidth(0.5),
.cornerRadius(radius: 10)
)
let thick = app.updating(.borderWidth(3))
let app = ViewStyle(
.borderWidth(0.5),
.cornerRadius(radius: 10)
)
let thick = app.updating(.borderWidth(3)).updating(.borderColor(.magenta))
To define your styling, there is nothing easier than creating multiple styles and combining them with +
operator.
let app = ViewStyle(
.borderWidth(0.5),
.cornerRadius(10)
)
let semiVisible = ViewStyle(
.opacity(0.5)
)
myLabel.viewStyle = app + semiVisible
let app = ViewStyle(
.borderWidth(0.5),
.cornerRadius(10)
)
let semiVisible = ViewStyle(
.opacity(0.5)
)
let blue = ViewStyle(
.backgroundColor(.blue)
)
let labelStyle = app + semiVisible + blue
myLabel.viewStyle = labelStyle