Created
July 5, 2024 18:22
-
-
Save teameh/08c3ed2dab6b548c7fb256db4cf0265e to your computer and use it in GitHub Desktop.
SwiftUI
This file contains hidden or 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
// | |
// CustomSpacer.swift | |
// | |
// Created by Tieme on 12/06/2024. | |
// | |
import SwiftUI | |
import Combine | |
struct CustomSpacer: View { | |
static var isHidden = CurrentValueSubject<Bool, Never>(false) | |
@State private var isHidden: Bool = false | |
var height: CGFloat? | |
var width: CGFloat? | |
@State var size: CGSize = .zero | |
var text: String { | |
[ | |
height != nil ? String(format: "%.1f", size.height) : nil, | |
width != nil ? String(format: "%.1f", size.width) : nil | |
] | |
.compactMap { $0 } | |
.joined(separator: "x") | |
} | |
var body: some View { | |
Group { | |
if let height { | |
Spacer().frame(height: height) | |
} else if let width { | |
Spacer().frame(width: width) | |
} else { | |
Spacer() | |
} | |
} | |
.overlay( | |
ZStack { | |
Rectangle() | |
.fill(.thinMaterial) | |
.frame(minWidth: width ?? 30, minHeight: height ?? 30) | |
.border(.regularMaterial) | |
.onGeometryChange( | |
for: CGSize.self, | |
of: { $0.size }, | |
action: { old, new in size = new } | |
) | |
Text(height != nil || width != nil ? "Variable" : text) | |
.font(.caption2) | |
.lineLimit(1) | |
.minimumScaleFactor(0.1) | |
.padding(4) | |
} | |
.opacity(isHidden ? 0 : 1) | |
) | |
.onReceive(CustomSpacer.isHidden) { self.isHidden = $0 } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment