Skip to content

Instantly share code, notes, and snippets.

@davidsteppenbeck
Created October 22, 2020 12:32
Show Gist options
  • Save davidsteppenbeck/6d417427651776aee1214ae30a8240c7 to your computer and use it in GitHub Desktop.
Save davidsteppenbeck/6d417427651776aee1214ae30a8240c7 to your computer and use it in GitHub Desktop.
A SwiftUI label style for icons with dynamic colored backgrounds.
/// Sets a colored rounded rectangle behind the icon.
struct ColorIconLabelStyle: LabelStyle {
/// The color behind the icon.
var color: Color
/// The corner radius of the color behind the icon.
@ScaledMetric private var radius: CGFloat = 5
func makeBody(configuration: Configuration) -> some View {
Label {
configuration.title
} icon: {
ZStack {
RoundedRectangle(cornerRadius: radius)
.foregroundColor(color)
.aspectRatio(1.0, contentMode: .fit)
configuration
.icon
.foregroundColor(.white)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment