Last active
July 6, 2024 22:22
-
-
Save StewartLynch/e0c4be713f9c2b5b0bc8d05c08496b19 to your computer and use it in GitHub Desktop.
Custom LabeledContentStyles
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
struct TopAlignedLabeledContentStyle: LabeledContentStyle { | |
// You can design it any way you want. | |
func makeBody(configuration: Configuration) -> some View { | |
HStack(alignment: .top) { | |
configuration.label | |
Spacer() | |
configuration.content | |
.foregroundStyle(.secondary) | |
} | |
} | |
} | |
extension LabeledContentStyle where Self == TopAlignedLabeledContentStyle { | |
static var topAligned: TopAlignedLabeledContentStyle { .init() } | |
} | |
struct BottomAlignedLabeledContentStyle: LabeledContentStyle { | |
// You can design it any way you want. | |
func makeBody(configuration: Configuration) -> some View { | |
HStack(alignment: .bottom) { | |
configuration.label | |
Spacer() | |
configuration.content | |
.foregroundStyle(.secondary) | |
} | |
} | |
} | |
extension LabeledContentStyle where Self == BottomAlignedLabeledContentStyle { | |
static var bottomAligned: BottomAlignedLabeledContentStyle { .init() } | |
} | |
struct StackedLeadingLabeledContentStyle: LabeledContentStyle { | |
// You can design it any way you want. | |
func makeBody(configuration: Configuration) -> some View { | |
VStack(alignment: .leading) { | |
configuration.label | |
configuration.content | |
.foregroundStyle(.secondary) | |
} | |
.frame(maxWidth: .infinity, alignment: .leading) | |
} | |
} | |
extension LabeledContentStyle where Self == StackedLeadingLabeledContentStyle { | |
static var stackedLeading: StackedLeadingLabeledContentStyle { .init() } | |
} | |
struct StackedTrailingLabeledContentStyle: LabeledContentStyle { | |
// You can design it any way you want. | |
func makeBody(configuration: Configuration) -> some View { | |
VStack(alignment: .trailing) { | |
configuration.label | |
configuration.content | |
.foregroundStyle(.secondary) | |
} | |
.frame(maxWidth: .infinity, alignment: .trailing) | |
} | |
} | |
extension LabeledContentStyle where Self == StackedTrailingLabeledContentStyle { | |
static var stackedTrailing: StackedTrailingLabeledContentStyle { .init() } | |
} | |
/* | |
// Example usaage | |
import SwiftUI | |
struct ContentView: View { | |
let text = "Some very long text that is likely to wrap" | |
var body: some View { | |
NavigationStack{ | |
VStack { | |
LabeledContent("Normal") { | |
Text(text) | |
} | |
Divider() | |
LabeledContent("Top Aligned") { | |
Text(text) | |
} | |
.labeledContentStyle(.topAligned) | |
Divider() | |
LabeledContent("Bottom") { | |
Text(text) | |
} | |
.labeledContentStyle(.bottomAligned) | |
Divider() | |
LabeledContent("Stacked Leading") { | |
Text(text) | |
} | |
.labeledContentStyle(.stackedLeading) | |
Divider() | |
LabeledContent("Stacked Trailing") { | |
Text(text) | |
} | |
.labeledContentStyle(.stackedTrailing) | |
Spacer() | |
} | |
.padding() | |
.navigationTitle("LabeledContentStyles") | |
} | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment