64 lines
1.2 KiB
Swift
64 lines
1.2 KiB
Swift
import SwiftUI
|
|
|
|
public struct TextLabeledContent<Content: View, Label: View>: View {
|
|
|
|
@Environment(\.textLabelStyle) var style
|
|
|
|
let label: TextLabel<Label>
|
|
let content: () -> Content
|
|
|
|
public init(
|
|
label: TextLabel<Label>,
|
|
@ViewBuilder content: @escaping () -> Content
|
|
) {
|
|
self.content = content
|
|
self.label = label
|
|
}
|
|
|
|
public var body: some View {
|
|
LabeledContent {
|
|
content()
|
|
} label: {
|
|
label
|
|
.textLabelStyle(style)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension TextLabeledContent where Label == Text {
|
|
|
|
public init(
|
|
_ title: LocalizedStringKey,
|
|
@ViewBuilder content: @escaping () -> Content
|
|
) {
|
|
self.init(
|
|
label: TextLabel(title),
|
|
content: content
|
|
)
|
|
}
|
|
|
|
public init<S: StringProtocol>(
|
|
_ title: S,
|
|
@ViewBuilder content: @escaping () -> Content
|
|
) {
|
|
self.init(
|
|
label: TextLabel(title),
|
|
content: content
|
|
)
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
VStack {
|
|
TextLabeledContent("Label") { Text("Content") }
|
|
.textLabelStyle(.boldSecondary)
|
|
|
|
Grid {
|
|
TextLabeledContent("One") { Text("One-Content") }
|
|
TextLabeledContent("Two") { Text("Two-Content") }
|
|
}
|
|
.textLabelStyle(.boldSecondary)
|
|
.labeledContentStyle(.gridRow)
|
|
}
|
|
}
|