-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f24aa1a
commit 52dea7b
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
Binary file modified
BIN
+18.8 KB
(120%)
...proj/project.xcworkspace/xcuserdata/tahatesser.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
SwiftUIPlayground/Samples/CustomLayout/LayoutProtocolSample.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// LayoutProtocolSample.swift | ||
// SwiftUIPlayground | ||
// | ||
// Created by Taha Tesser on 10/14/24. | ||
// | ||
|
||
// Reference https://sarunw.com/posts/swiftui-custom-layout/ | ||
|
||
import SwiftUI | ||
|
||
struct LayoutProtocolSample: View { | ||
var body: some View { | ||
BackslashStack { | ||
Text("Lorem") | ||
.border(.yellow) | ||
Text("eveniet facilis") | ||
.border(.blue) | ||
Text("possimus eaque adipisci ") | ||
.border(.green) | ||
} | ||
.border(.pink) | ||
} | ||
} | ||
|
||
#Preview { | ||
LayoutProtocolSample() | ||
} | ||
|
||
struct BackslashStack: Layout { | ||
|
||
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { | ||
|
||
let subviewSizes = subviews.map { proxy in | ||
return proxy.sizeThatFits(.unspecified) | ||
} | ||
|
||
let combinedSize = subviewSizes.reduce(.zero) { currentSize, subviewSize in | ||
|
||
return CGSize( | ||
width: currentSize.width + subviewSize.width, height: currentSize.height + subviewSize.height) | ||
} | ||
|
||
return combinedSize | ||
} | ||
|
||
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { | ||
|
||
|
||
let subviewSizes = subviews.map { proxy in | ||
return proxy.sizeThatFits(.unspecified) | ||
} | ||
|
||
var x = bounds.minX | ||
var y = bounds.minY | ||
|
||
for index in subviews.indices { | ||
let subviewSize = subviewSizes[index] | ||
let sizeProposal = ProposedViewSize( | ||
width: subviewSize.width, | ||
height: subviewSize.height) | ||
|
||
subviews[index] | ||
.place( | ||
at: CGPoint(x: x, y: y), | ||
proposal: sizeProposal) | ||
|
||
x += subviewSize.width | ||
y += subviewSize.height | ||
} | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
SwiftUIPlayground/Samples/ViewFundamentals/ViewModifierProtocolSample.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// ViewModifierProtocolSamp,e.swift | ||
// SwiftUIPlayground | ||
// | ||
// Created by Taha Tesser on 10/14/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ViewModifierProtocolSample: View { | ||
@State var text: String = "Hello, World!" | ||
|
||
|
||
var body: some View { | ||
TextField( | ||
"", | ||
text: $text, | ||
prompt: Text("Placeholder")) | ||
.clearButton( | ||
text: $text, | ||
onClearHandler: { | ||
print("Text wad cleared!") | ||
|
||
}) | ||
// .modifier(ClearTextButtonViewModifier(text: $text, onClearHandler: { | ||
// print("Text wad cleared!") | ||
// })) | ||
.padding() | ||
} | ||
} | ||
|
||
#Preview { | ||
ViewModifierProtocolSample() | ||
} | ||
|
||
extension View { | ||
|
||
func clearButton( | ||
text: Binding<String>, | ||
onClearHandler: (() -> Void)?) -> some View { | ||
modifier(ClearTextButtonViewModifier(text: text, onClearHandler: onClearHandler)) | ||
} | ||
} | ||
|
||
private struct ClearTextButtonViewModifier: ViewModifier { | ||
@Binding var text: String | ||
|
||
let onClearHandler: (() -> Void)? | ||
|
||
func body(content: Content) -> some View { | ||
ZStack { | ||
content | ||
|
||
HStack { | ||
Spacer() | ||
|
||
Button { | ||
text.removeAll() | ||
|
||
onClearHandler?() | ||
} label: { | ||
Image(systemName: "xmark.circle.fill") | ||
.foregroundStyle(.placeholder) | ||
.padding(.trailing, 10) | ||
} | ||
.buttonStyle(.plain) | ||
} | ||
.opacity(text.isEmpty ? 0.0 : 1.0) | ||
} | ||
} | ||
} | ||
|