Created
February 16, 2022 14:49
-
-
Save ericlewis/da489a57c86cac1d70914382a86d0c2e to your computer and use it in GitHub Desktop.
Initializer overloads to make working with labels easier.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension Toggle where Label == SwiftUI.Label<Text, Image> { | |
public init(_ titleKey: LocalizedStringKey, systemImage named: String, isOn: Binding<Bool>) { | |
self.init(isOn: isOn, label: { Label(titleKey, systemImage: named) }) | |
} | |
public init(_ titleKey: LocalizedStringKey, image named: String, isOn: Binding<Bool>) { | |
self.init(isOn: isOn, label: { Label(titleKey, image: named) }) | |
} | |
public init<S>(_ title: S, systemImage named: String, isOn: Binding<Bool>) where S: StringProtocol { | |
self.init(isOn: isOn, label: { Label(title, systemImage: named) }) | |
} | |
public init<S>(_ title: S, image named: String, isOn: Binding<Bool>) where S: StringProtocol { | |
self.init(isOn: isOn, label: { Label(title, image: named) }) | |
} | |
} | |
extension Button where Label == SwiftUI.Label<Text, Image> { | |
public init(_ titleKey: LocalizedStringKey, systemImage named: String, action: @escaping () -> Void) { | |
self.init(action: action, label: { Label(titleKey, systemImage: named) }) | |
} | |
public init(_ titleKey: LocalizedStringKey, image named: String, action: @escaping () -> Void) { | |
self.init(action: action, label: { Label(titleKey, image: named) }) | |
} | |
public init<S>(_ title: S, systemImage named: String, action: @escaping () -> Void) where S : StringProtocol { | |
self.init(action: action, label: { Label(title, systemImage: named) }) | |
} | |
public init<S>(_ title: S, image named: String, action: @escaping () -> Void) where S : StringProtocol { | |
self.init(action: action, label: { Label(title, image: named) }) | |
} | |
} | |
extension Picker where Label == SwiftUI.Label<Text, Image> { | |
public init(_ titleKey: LocalizedStringKey, systemImage named: String, selection: Binding<SelectionValue>, @ViewBuilder content: () -> Content) { | |
self.init(selection: selection, content: content, label: { Label(titleKey, systemImage: named) }) | |
} | |
public init(_ titleKey: LocalizedStringKey, image named: String, selection: Binding<SelectionValue>, @ViewBuilder content: () -> Content) { | |
self.init(selection: selection, content: content, label: { Label(titleKey, image: named) }) | |
} | |
public init<S>(_ title: S, systemImage named: String, selection: Binding<SelectionValue>, @ViewBuilder content: () -> Content) where S : StringProtocol { | |
self.init(selection: selection, content: content, label: { Label(title, systemImage: named) }) | |
} | |
public init<S>(_ title: S, image named: String, selection: Binding<SelectionValue>, @ViewBuilder content: () -> Content) where S : StringProtocol { | |
self.init(selection: selection, content: content, label: { Label(title, image: named) }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also do this if you want even more control: