Created
April 5, 2024 00:59
-
-
Save amosgyamfi/e7f32cb2ffd91dfe71bd8bc1faf30950 to your computer and use it in GitHub Desktop.
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
// | |
// 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