Skip to content

Commit 329c667

Browse files
committed
Add LabelsHiddenModifier
1 parent 55dfa9f commit 329c667

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// LabelsHiddenModifierUITests.swift
3+
// OpenSwiftUIUITests
4+
5+
import Testing
6+
import SnapshotTesting
7+
8+
@MainActor
9+
@Suite(.snapshots(record: .never, diffTool: diffTool))
10+
struct LabelsHiddenModifierUITests {
11+
@Test
12+
func toggleLabelsHidden() {
13+
struct ContentView: View {
14+
@State private var toggle1 = false
15+
@State private var toggle2 = false
16+
var body: some View {
17+
VStack {
18+
Toggle(isOn: $toggle1) {
19+
// Text("Toggle 1")
20+
Color.red
21+
}
22+
.labelsHidden()
23+
Toggle(isOn: $toggle2) {
24+
// Text("Toggle 2")
25+
Color.blue
26+
}
27+
}
28+
}
29+
}
30+
#if os(iOS) || os(visionOS)
31+
openSwiftUIAssertSnapshot(of: ContentView())
32+
#else
33+
withKnownIssue("checkBox style is not supported yet") {
34+
openSwiftUIAssertSnapshot(of: ContentView())
35+
}
36+
#endif
37+
}
38+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
/// ![A screenshot showing a view with two toggle controls where one label
35+
/// is visible and the other label is hidden.](View-labelsHidden-1.png)
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

Comments
 (0)