Skip to content

Instantly share code, notes, and snippets.

@ericlewis
Created February 16, 2022 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericlewis/da489a57c86cac1d70914382a86d0c2e to your computer and use it in GitHub Desktop.
Save ericlewis/da489a57c86cac1d70914382a86d0c2e to your computer and use it in GitHub Desktop.
Initializer overloads to make working with labels easier.
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) })
}
}
@ericlewis
Copy link
Author

ericlewis commented Feb 16, 2022

You can also do this if you want even more control:

extension Toggle {
  public init<Title: View, Icon: View>(@ViewBuilder title: () -> Title, @ViewBuilder icon: () -> Icon, isOn: Binding<Bool>) where Label == SwiftUI.Label<Title, Icon> {
    self.init(isOn: isOn, label: { SwiftUI.Label(title: title, icon: icon) })
  }

  public init<Title: View, Icon: View>(title: Title, icon: Icon, isOn: Binding<Bool>) where Label == SwiftUI.Label<Title, Icon> {
    self.init(isOn: isOn, label: { SwiftUI.Label(title: { title }, icon: { icon }) })
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment