Last active
May 15, 2025 14:11
-
-
Save dreymonde/69f16adcacfaf52e25cebeed314f2dff to your computer and use it in GitHub Desktop.
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
| extension View { | |
| @available(*, deprecated, message: "Only use during development") | |
| func debugBorder(_ color: Color = .green) -> some View { | |
| #if DEBUG | |
| modifier(DebugBorderModifier(color: color)) | |
| #else | |
| self | |
| #endif | |
| } | |
| } | |
| struct DebugBorderModifier: ViewModifier { | |
| let color: Color | |
| @State private var size: CGSize? | |
| func body(content: Content) -> some View { | |
| content.background( | |
| GeometryReader { geometry in | |
| Color.clear | |
| .onAppear { | |
| self.size = geometry.size | |
| } | |
| .onChange(of: geometry.size) { _, newSize in | |
| self.size = newSize | |
| } | |
| } | |
| ) | |
| .border(color.secondary) | |
| .overlay( | |
| alignment: .top, | |
| content: { | |
| sizeOverlayBox | |
| .alignmentGuide(.top, computeValue: { $0[.bottom] }) | |
| } | |
| ) | |
| } | |
| @ViewBuilder | |
| private var sizeOverlayBox: some View { | |
| if let size { | |
| Text("\(Int(size.width))✕\(Int(size.height))") | |
| .font(.system(size: 6, weight: .semibold, design: .monospaced)) | |
| .foregroundStyle(.black) | |
| .padding(2) | |
| .background(color) | |
| } | |
| } | |
| } | |
| public extension Color { | |
| static func random(randomOpacity: Bool = false) -> Color { | |
| Color( | |
| red: .random(in: 0...1), | |
| green: .random(in: 0...1), | |
| blue: .random(in: 0...1), | |
| opacity: randomOpacity ? .random(in: 0...1) : 1 | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment