@@ -123,40 +123,62 @@ extension Backport where Wrapped == Any {
123123 ///
124124 /// You can set label styles using the ``View/labeledContentStyle(_:)``
125125 /// modifier. You can also build custom styles using ``LabeledContentStyle``.
126- public struct LabeledContent < Label, Content> {
126+ public struct LabeledContent < Label, Content> : View {
127+ @EnvironmentContains ( key: " LabelsHiddenKey " ) private var isHidden
127128 @Environment ( \. backportLabeledContentStyle) private var style
129+
128130 let config : LabeledContentStyleConfiguration
131+
129132 public var body : some View {
130- style. makeBody ( configuration: config)
133+ style. makeBody ( configuration: config. labelHidden ( isHidden) )
134+ }
135+
136+ /// Creates labeled content based on a labeled content style configuration.
137+ ///
138+ /// You can use this initializer within the
139+ /// ``LabeledContentStyle/makeBody(configuration:)`` method of a
140+ /// ``LabeledContentStyle`` to create a labeled content instance.
141+ /// This is useful for custom styles that only modify the current style,
142+ /// as opposed to implementing a brand new style.
143+ ///
144+ /// For example, the following style adds a red border around the labeled
145+ /// content, but otherwise preserves the current style:
146+ ///
147+ /// struct RedBorderLabeledContentStyle: LabeledContentStyle {
148+ /// func makeBody(configuration: Configuration) -> some View {
149+ /// LabeledContent(configuration)
150+ /// .border(.red)
151+ /// }
152+ /// }
153+ ///
154+ /// - Parameter configuration: The properties of the labeled content
155+ public init ( _ config: Backport . LabeledContentStyleConfiguration ) {
156+ self . config = config
131157 }
132158 }
133159
134160}
135161
136- extension Backport . LabeledContent where Wrapped == Any , Label == Backport < Any > . LabeledContentStyleConfiguration . Label , Content == Backport < Any > . LabeledContentStyleConfiguration . Content {
162+ extension Backport . LabeledContent where Wrapped == Any , Label: View , Content: View {
137163
138- /// Creates labeled content based on a labeled content style configuration.
139- ///
140- /// You can use this initializer within the
141- /// ``LabeledContentStyle/makeBody(configuration:)`` method of a
142- /// ``LabeledContentStyle`` to create a labeled content instance.
143- /// This is useful for custom styles that only modify the current style,
144- /// as opposed to implementing a brand new style.
145- ///
146- /// For example, the following style adds a red border around the labeled
147- /// content, but otherwise preserves the current style:
164+ /// Creates a labeled view that generates its label from a localized string
165+ /// key.
148166 ///
149- /// struct RedBorderLabeledContentStyle: LabeledContentStyle {
150- /// func makeBody(configuration: Configuration) -> some View {
151- /// LabeledContent(configuration)
152- /// .border(.red)
153- /// }
154- /// }
167+ /// This initializer creates a ``Text`` label on your behalf, and treats the
168+ /// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. See
169+ /// `Text` for more information about localizing strings.
155170 ///
156- /// - Parameter configuration: The properties of the labeled content
157- public init ( _ config: Backport . LabeledContentStyleConfiguration ) {
158- self . config = config
171+ /// - Parameters:
172+ /// - titleKey: The key for the view's localized title, that describes
173+ /// the purpose of the view.
174+ /// - content: The value content being labeled.
175+ public init ( @ViewBuilder content: ( ) -> Content , @ViewBuilder label: ( ) -> Label ) {
176+ config = . init(
177+ label: label ( ) ,
178+ content: content ( )
179+ )
159180 }
181+
160182}
161183
162184extension Backport . LabeledContent where Wrapped == Any , Label == Text , Content : View {
@@ -173,10 +195,11 @@ extension Backport.LabeledContent where Wrapped == Any, Label == Text, Content :
173195 /// the purpose of the view.
174196 /// - content: The value content being labeled.
175197 public init ( _ titleKey: LocalizedStringKey , @ViewBuilder content: ( ) -> Content ) {
176- config = . init(
177- label: Text ( titleKey) ,
178- content: content ( )
179- )
198+ self . init {
199+ content ( )
200+ } label: {
201+ Text ( titleKey)
202+ }
180203 }
181204
182205 /// Creates a labeled view that generates its label from a string.
@@ -189,32 +212,11 @@ extension Backport.LabeledContent where Wrapped == Any, Label == Text, Content :
189212 /// - title: A string that describes the purpose of the view.
190213 /// - content: The value content being labeled.
191214 public init < S> ( _ title: S , @ViewBuilder content: ( ) -> Content ) where S: StringProtocol {
192- config = . init(
193- label: Text ( title) ,
194- content: content ( )
195- )
196- }
197-
198- }
199-
200- extension Backport . LabeledContent : View where Wrapped == Any , Label: View , Content: View {
201-
202- /// Creates a labeled view that generates its label from a localized string
203- /// key.
204- ///
205- /// This initializer creates a ``Text`` label on your behalf, and treats the
206- /// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. See
207- /// `Text` for more information about localizing strings.
208- ///
209- /// - Parameters:
210- /// - titleKey: The key for the view's localized title, that describes
211- /// the purpose of the view.
212- /// - content: The value content being labeled.
213- public init ( @ViewBuilder content: ( ) -> Content , @ViewBuilder label: ( ) -> Label ) {
214- config = . init(
215- label: label ( ) ,
216- content: content ( )
217- )
215+ self . init {
216+ content ( )
217+ } label: {
218+ Text ( title)
219+ }
218220 }
219221
220222}
@@ -238,9 +240,11 @@ extension Backport.LabeledContent where Wrapped == Any, Label == Text, Content =
238240 /// the purpose of the view.
239241 /// - value: The value being labeled.
240242 public init < S: StringProtocol > ( _ titleKey: LocalizedStringKey , value: S ) {
241- config = . init(
242- label: Text ( titleKey) ,
243- content: Text ( value) )
243+ self . init {
244+ Text ( value)
245+ } label: {
246+ Text ( titleKey)
247+ }
244248 }
245249
246250 /// Creates a labeled informational view.
@@ -259,10 +263,11 @@ extension Backport.LabeledContent where Wrapped == Any, Label == Text, Content =
259263 /// - title: A string that describes the purpose of the view.
260264 /// - value: The value being labeled.
261265 public init < S1: StringProtocol , S2: StringProtocol > ( _ title: S1 , value: S2 ) {
262- config = . init(
263- label: Text ( title) ,
264- content: Text ( value)
265- )
266+ self . init {
267+ Text ( value)
268+ } label: {
269+ Text ( title)
270+ }
266271 }
267272
268273}
0 commit comments