Skip to content

Instantly share code, notes, and snippets.

@amosgyamfi
Created April 5, 2024 00:59
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 amosgyamfi/e7f32cb2ffd91dfe71bd8bc1faf30950 to your computer and use it in GitHub Desktop.
Save amosgyamfi/e7f32cb2ffd91dfe71bd8bc1faf30950 to your computer and use it in GitHub Desktop.
//
// LabelView.swift
// OpenSwiftUIDesigns
//
// Created by Amos Gyamfi on 4.4.2024.
//
import SwiftUI
struct LabelView: View {
var body: some View {
List {
// 1. Request icon and label
Section {
Label("Lightning", systemImage: "bolt.fill")
} header: {
Text("1. Request icon and label")
}
// 2. Request title only
Section {
Label("Lightning", systemImage: "bolt.fill")
.labelStyle(.titleOnly)
} header: {
Text("2. Request title only")
}
// 3. Request icon only
Section {
Label("Lightning", systemImage: "bolt.fill")
.labelStyle(.iconOnly)
} header: {
Text("3. Request icon only")
}
// 4. Add a custom label style
Section {
Label("Lightning", systemImage: "bolt.fill")
.labelStyle(GradientBorderedLabelStyle())
} header: {
Text("4. Add a custom label style")
}
// 5. Add a label style to a group of labels
Section {
HStack {
Label("Rain", systemImage: "cloud.rain")
Label("Snow", systemImage: "snow")
Label("Sun", systemImage: "sun.max")
}
.labelStyle(GradientBorderedLabelStyle())
.labelStyle(.iconOnly)
} header: {
Text("5. Add a label style to a group of labels")
}
// 6. Make labels using views to compose the label’s icon programmatically
Section {
Label {
Text("Amos Gyamfi")
.font(.body)
.foregroundColor(.primary)
Text("Developer Advocate")
.font(.subheadline)
.foregroundColor(.secondary)
} icon: {
Circle()
.fill(.blue.gradient)
.frame(width: 44, height: 44, alignment: .center)
.overlay(Text("AG").foregroundStyle(.white))
}
} header: {
Text("6. Make labels using views to compose the label’s icon programmatically")
}
}
}
}
struct GradientBorderedLabelStyle: LabelStyle {
func makeBody(configuration: Configuration) -> some View {
Label(configuration)
.padding(8)
.border(
LinearGradient(
gradient: Gradient(colors: [Color.red, Color.blue]),
startPoint: .leading,
endPoint: .trailing)
)
}
}
#Preview {
LabelView()
.preferredColorScheme(.dark)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment