Skip to content

Instantly share code, notes, and snippets.

@teameh
Created July 5, 2024 18:22
Show Gist options
  • Save teameh/08c3ed2dab6b548c7fb256db4cf0265e to your computer and use it in GitHub Desktop.
Save teameh/08c3ed2dab6b548c7fb256db4cf0265e to your computer and use it in GitHub Desktop.
SwiftUI
//
// 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