-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlPlaygrounds.swift
More file actions
199 lines (192 loc) · 7.2 KB
/
Copy pathControlPlaygrounds.swift
File metadata and controls
199 lines (192 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#if canImport(AndroidSwiftUI)
import AndroidSwiftUI
#else
import SwiftUI
#endif
struct TextPlayground: View {
@State private var counter = 0
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Example("Plain") { Text("Hello, world") }
Example("Verbatim") { Text(verbatim: "Raw string, no interpolation") }
Example("Interpolated") { Text("Counter is \(counter)") }
Example("Bump the counter") { Button("Increment") { counter += 1 } }
Example("Font sizes") {
VStack(alignment: .leading, spacing: 4) {
Text("Large title").font(.largeTitle)
Text("Title").font(.title)
Text("Headline").font(.headline)
Text("Body").font(.body)
Text("Caption").font(.caption)
}
}
Example("Weight & style") {
VStack(alignment: .leading, spacing: 4) {
Text("Bold").bold()
Text("Semibold").fontWeight(.semibold)
Text("Italic").italic()
Text("System 24 heavy").font(.system(size: 24, weight: .heavy))
}
}
Example("Colors") {
VStack(alignment: .leading, spacing: 4) {
Text("Red").foregroundColor(.red)
Text("Blue").foregroundColor(.blue)
Text("Green, bold, title").font(.title).bold().foregroundColor(.green)
}
}
Example("Line limit") {
Text("A longer passage of text that wraps onto multiple lines, capped at two by lineLimit so the rest is truncated.")
.lineLimit(2)
}
Example("Multiline") {
Text("A longer passage of text that wraps onto multiple lines when it no longer fits within the width of the screen.")
}
Example("Styled") {
Text("Blue on a rounded chip")
.foregroundColor(.white)
.padding()
.background(Color.blue)
.cornerRadius(12)
}
}
}
}
}
struct ButtonPlayground: View {
@State private var taps = 0
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Example("Taps: \(taps)") { Text("Tap any button below") }
Example("Title initializer") { Button("Tap me") { taps += 1 } }
Example("Label closure") {
Button(action: { taps += 1 }) { Text("Custom label") }
}
Example("Styled label") {
Button(action: { taps += 1 }) {
Text("Padded label")
.padding()
.background(Color.green)
.cornerRadius(8)
}
}
Example("Reset") { Button("Reset to zero") { taps = 0 } }
}
}
}
}
struct TogglePlayground: View {
@State private var a = false
@State private var b = true
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Example("Off by default") {
VStack(alignment: .leading, spacing: 8) {
Toggle("Enable feature", isOn: $a)
Text(a ? "Feature is on" : "Feature is off")
}
}
Example("On by default") {
VStack(alignment: .leading, spacing: 8) {
Toggle("Notifications", isOn: $b)
Text(b ? "Notifications on" : "Notifications off")
}
}
}
}
}
}
struct SliderPlayground: View {
@State private var unit = 0.5
@State private var wide = 25.0
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Example("0...1") {
VStack(alignment: .leading, spacing: 8) {
Slider(value: $unit, in: 0...1)
Text("Value: \(Int(unit * 100))%")
}
}
Example("0...100") {
VStack(alignment: .leading, spacing: 8) {
Slider(value: $wide, in: 0...100)
Text("Value: \(Int(wide))")
}
}
}
}
}
}
struct TextFieldPlayground: View {
@State private var name = ""
@State private var city = ""
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Example("Name") {
VStack(alignment: .leading, spacing: 8) {
TextField("Your name", text: $name)
Text(name.isEmpty ? "Nothing typed yet" : "Hello, \(name)")
}
}
Example("City") {
VStack(alignment: .leading, spacing: 8) {
TextField("Your city", text: $city)
Text(city.isEmpty ? "—" : "You are in \(city)")
}
}
}
}
}
}
struct PickerPlayground: View {
@State private var fruit = "Apple"
@State private var size = 1
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Example("String selection") {
VStack(alignment: .leading, spacing: 8) {
Picker("Fruit", selection: $fruit) {
Text("Apple").tag("Apple")
Text("Banana").tag("Banana")
Text("Cherry").tag("Cherry")
}
Text("Selected: \(fruit)")
}
}
Example("Int selection") {
VStack(alignment: .leading, spacing: 8) {
Picker("Size", selection: $size) {
Text("Small").tag(0)
Text("Medium").tag(1)
Text("Large").tag(2)
}
Text("Size index: \(size)")
}
}
}
}
}
}
struct ProgressViewPlayground: View {
@State private var progress = 0.25
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Example("Indeterminate") { ProgressView() }
Example("Determinate") {
VStack(alignment: .leading, spacing: 8) {
ProgressView(value: progress)
Text("\(Int(progress * 100))%")
Button("Advance") { progress = progress >= 1 ? 0 : progress + 0.25 }
}
}
}
}
}
}