Skip to content

Instantly share code, notes, and snippets.

@kirsteins
Created April 18, 2024 12:43
Show Gist options
  • Save kirsteins/62bd716dd03db68d402f5e6157b96e05 to your computer and use it in GitHub Desktop.
Save kirsteins/62bd716dd03db68d402f5e6157b96e05 to your computer and use it in GitHub Desktop.
CustomLabelStyle that mimics List+Label
struct CustomLabelStyle: LabelStyle {
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
private var leadingPadding: CGFloat {
switch dynamicTypeSize {
case .xSmall: -8
case .small: -9
case .medium: -9
case .large: -10
case .xLarge: -11
case .xxLarge: -13
case .xxxLarge: -14
case .accessibility1: -18
case .accessibility2: -21
case .accessibility3: -25
case .accessibility4: -31
case .accessibility5: -35
@unknown default: -10
}
}
private var spacing: CGFloat {
switch dynamicTypeSize {
case .xSmall: 9
case .small: 8
case .medium: 7
case .large: 6
case .xLarge: 5
case .xxLarge: 4
case .xxxLarge: 3
case .accessibility1: -1
case .accessibility2: -5
case .accessibility3: -10
case .accessibility4: -14
case .accessibility5: -18
@unknown default: 6
}
}
func makeBody(configuration: Configuration) -> some View {
HStack(spacing: spacing) {
ZStack {
// Hidden image that reserves width for one of the widest sfsymbols
Image(systemName: "person.3.sequence.fill")
.hidden()
.accessibilityHidden(true)
configuration.icon
}
.padding(.leading, leadingPadding)
.imageScale(.large)
.foregroundStyle(Color.accentColor)
configuration.title
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment