Label ⟩ custom styles
👉 SwiftUI ⟩ Label
struct ContentView: View {
@State private var isOn = true
var body: some View {
Toggle(isOn: $isOn){
Label("Hello", systemImage: "person")
// 🌀 .redBorder
.labelStyle(.redBorder)
}
}
}
// ⭐️ custom label style
struct RedBorderedLabelStyle: LabelStyle {
// ⭐️ protocol's only requirement
func makeBody(configuration: Configuration) -> some View {
Label(configuration)
.padding(4)
.border(Color.red)
}
}
// 🌀 .redBorder
// ╭───────── ⭐️ important ──────────╮
extension LabelStyle where Self == RedBorderedLabelStyle {
// extension supports "computed properties" (no "stored properties")
public static var redBorder: RedBorderedLabelStyle {
RedBorderedLabelStyle()
}
}
SwiftUI ⟩ Views and Controls ⟩ Label ⟩
LabelStyle (protocol) ⟩ makeBody(configuration:)
Last updated