Skip to content

Commit 52dea7b

Browse files
committed
More samples
1 parent f24aa1a commit 52dea7b

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

SwiftUIPlayground/Samples.swift

+14
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,18 @@ public let samples: [String: AnyView] = [
152152
"matchedgeometryeffect": AnyView(MatchedGeometryEffectSample()),
153153
]
154154
)),
155+
"Custom Layout": AnyView(SamplesList(
156+
title: "Custom Layout", samples:
157+
[
158+
// https://developer.apple.com/documentation/swiftui/layout
159+
"Layout Protocol": AnyView(LayoutProtocolSample()),
160+
]
161+
)),
162+
"View Fundamentals": AnyView(SamplesList(
163+
title: "View Fundamental", samples:
164+
[
165+
// https://developer.apple.com/documentation/swiftui/viewmodifier
166+
"ViewModifier Protocol": AnyView(ViewModifierProtocolSample()),
167+
]
168+
)),
155169
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// LayoutProtocolSample.swift
3+
// SwiftUIPlayground
4+
//
5+
// Created by Taha Tesser on 10/14/24.
6+
//
7+
8+
// Reference https://sarunw.com/posts/swiftui-custom-layout/
9+
10+
import SwiftUI
11+
12+
struct LayoutProtocolSample: View {
13+
var body: some View {
14+
BackslashStack {
15+
Text("Lorem")
16+
.border(.yellow)
17+
Text("eveniet facilis")
18+
.border(.blue)
19+
Text("possimus eaque adipisci ")
20+
.border(.green)
21+
}
22+
.border(.pink)
23+
}
24+
}
25+
26+
#Preview {
27+
LayoutProtocolSample()
28+
}
29+
30+
struct BackslashStack: Layout {
31+
32+
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
33+
34+
let subviewSizes = subviews.map { proxy in
35+
return proxy.sizeThatFits(.unspecified)
36+
}
37+
38+
let combinedSize = subviewSizes.reduce(.zero) { currentSize, subviewSize in
39+
40+
return CGSize(
41+
width: currentSize.width + subviewSize.width, height: currentSize.height + subviewSize.height)
42+
}
43+
44+
return combinedSize
45+
}
46+
47+
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
48+
49+
50+
let subviewSizes = subviews.map { proxy in
51+
return proxy.sizeThatFits(.unspecified)
52+
}
53+
54+
var x = bounds.minX
55+
var y = bounds.minY
56+
57+
for index in subviews.indices {
58+
let subviewSize = subviewSizes[index]
59+
let sizeProposal = ProposedViewSize(
60+
width: subviewSize.width,
61+
height: subviewSize.height)
62+
63+
subviews[index]
64+
.place(
65+
at: CGPoint(x: x, y: y),
66+
proposal: sizeProposal)
67+
68+
x += subviewSize.width
69+
y += subviewSize.height
70+
}
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// ViewModifierProtocolSamp,e.swift
3+
// SwiftUIPlayground
4+
//
5+
// Created by Taha Tesser on 10/14/24.
6+
//
7+
8+
import SwiftUI
9+
10+
struct ViewModifierProtocolSample: View {
11+
@State var text: String = "Hello, World!"
12+
13+
14+
var body: some View {
15+
TextField(
16+
"",
17+
text: $text,
18+
prompt: Text("Placeholder"))
19+
.clearButton(
20+
text: $text,
21+
onClearHandler: {
22+
print("Text wad cleared!")
23+
24+
})
25+
// .modifier(ClearTextButtonViewModifier(text: $text, onClearHandler: {
26+
// print("Text wad cleared!")
27+
// }))
28+
.padding()
29+
}
30+
}
31+
32+
#Preview {
33+
ViewModifierProtocolSample()
34+
}
35+
36+
extension View {
37+
38+
func clearButton(
39+
text: Binding<String>,
40+
onClearHandler: (() -> Void)?) -> some View {
41+
modifier(ClearTextButtonViewModifier(text: text, onClearHandler: onClearHandler))
42+
}
43+
}
44+
45+
private struct ClearTextButtonViewModifier: ViewModifier {
46+
@Binding var text: String
47+
48+
let onClearHandler: (() -> Void)?
49+
50+
func body(content: Content) -> some View {
51+
ZStack {
52+
content
53+
54+
HStack {
55+
Spacer()
56+
57+
Button {
58+
text.removeAll()
59+
60+
onClearHandler?()
61+
} label: {
62+
Image(systemName: "xmark.circle.fill")
63+
.foregroundStyle(.placeholder)
64+
.padding(.trailing, 10)
65+
}
66+
.buttonStyle(.plain)
67+
}
68+
.opacity(text.isEmpty ? 0.0 : 1.0)
69+
}
70+
}
71+
}
72+

0 commit comments

Comments
 (0)