Files
2024-06-05 22:27:43 -04:00

58 lines
1.3 KiB
Swift

import SwiftUI
/// A view that can be styled view the `.textLabelStyle` modifier, generally they will be
/// simple `Text` views, however it will accept any content view and will apply the style to.
///
/// Custom styles can be created by conforming to the ``TextLabelStyle`` protocol.
///
public struct TextLabel<Content: View>: View {
@Environment(\.textLabelStyle) private var textLabelStyle
private let content: () -> Content
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
public var body: some View {
textLabelStyle.makeBody(
configuration: TextLabelConfiguration(
label: TextLabelConfiguration.Label(content: content())
)
)
}
}
extension TextLabel where Content == Text {
public init(_ text: LocalizedStringKey) {
self.init { Text(text) }
}
public init<S>(_ text: S) where S: StringProtocol {
self.init { Text(text) }
}
}
#Preview {
VStack {
TextLabel("Automatic")
TextLabel("Secondary-Bold")
.textLabelStyle(AnyTextLabelStyle.boldSecondary)
TextLabel("Secondary")
.textLabelStyle(.secondary)
.padding(.bottom)
Group {
TextLabel("One")
TextLabel("Two")
TextLabel("Three")
}
.font(.title)
.textLabelStyle(.boldSecondary)
}
}