|
| 1 | +// |
| 2 | +// LabelsHiddenModifier.swift |
| 3 | +// OpenSwiftUI |
| 4 | +// |
| 5 | +// Audited for 6.5.4 |
| 6 | +// Status: Complete |
| 7 | + |
| 8 | +public import OpenSwiftUICore |
| 9 | + |
| 10 | +@available(OpenSwiftUI_v1_0, *) |
| 11 | +extension View { |
| 12 | + |
| 13 | + /// Hides the labels of any controls contained within this view. |
| 14 | + /// |
| 15 | + /// Use this modifier when you want to omit a label from one or more |
| 16 | + /// controls in your user interface. For example, the first ``Toggle`` in |
| 17 | + /// the following example hides its label: |
| 18 | + /// |
| 19 | + /// VStack { |
| 20 | + /// Toggle(isOn: $toggle1) { |
| 21 | + /// Text("Toggle 1") |
| 22 | + /// } |
| 23 | + /// .labelsHidden() |
| 24 | + /// |
| 25 | + /// Toggle(isOn: $toggle2) { |
| 26 | + /// Text("Toggle 2") |
| 27 | + /// } |
| 28 | + /// } |
| 29 | + /// |
| 30 | + /// The ``VStack`` in the example above centers the first toggle's control |
| 31 | + /// element in the available space, while it centers the second toggle's |
| 32 | + /// combined label and control element: |
| 33 | + /// |
| 34 | + ///  |
| 36 | + /// |
| 37 | + /// Always provide a label for controls, even when you hide the label, |
| 38 | + /// because SwiftUI uses labels for other purposes, including accessibility. |
| 39 | + /// |
| 40 | + /// > Note: This modifier doesn't work for all labels. It applies to |
| 41 | + /// labels that are separate from the rest of the control's interface, |
| 42 | + /// like they are for ``Toggle``, but not to controls like a bordered |
| 43 | + /// button where the label is inside the button's border. |
| 44 | + nonisolated public func labelsHidden() -> some View { |
| 45 | + modifier(LabelsHiddenModifier()) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +struct LabelsHiddenModifier: ViewModifier { |
| 50 | + func body(content: Content) -> some View { |
| 51 | + content |
| 52 | + .labeledContentStyle(HiddenLabeledContentStyle()) |
| 53 | + .labelsVisibility(.hidden) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +struct HiddenLabeledContentStyle: LabeledContentStyle { |
| 58 | + func makeBody(configuration: Configuration) -> some View { |
| 59 | + configuration.content |
| 60 | + } |
| 61 | +} |
0 commit comments