Skip to content

Commit

Permalink
Merge pull request #7 from vdshko/develop
Browse files Browse the repository at this point in the history
Add customisation abilities
  • Loading branch information
vdshko authored Apr 6, 2023
2 parents 7fd2f81 + febf5a0 commit b08cabf
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 28 deletions.
2 changes: 1 addition & 1 deletion DebugFrame.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "DebugFrame"
spec.version = "0.0.2"
spec.version = "0.1.3"
spec.summary = "Debug frame modifier for SwiftUI views."
spec.homepage = "https://github.com/vdshko/DebugFrame.git"
spec.license = { :type => "MIT", :file => "License" }
Expand Down
10 changes: 5 additions & 5 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ContentView.swift
// Example
//
// Created by Vlad Shkodich on 19.12.2022.
// Created by Vladyslav Shkodych on 19.12.2022.
//

import SwiftUI
Expand All @@ -16,7 +16,7 @@ struct ContentView: View {
rectangles
Spacer()
}
.debugFrame()
.debugFrame(.size)
.padding(20.0)
.debugFrame()
.padding(30.0)
Expand All @@ -43,20 +43,20 @@ struct ContentView: View {
RoundedRectangle(cornerRadius: 4.0)
.frame(width: 220.0, height: 100.0)
.foregroundColor(Color("first"))
.debugFrame()
.debugFrame(color: .black)
HStack {
Spacer()
RoundedRectangle(cornerRadius: 4.0)
.frame(width: 120.0, height: 150.0)
.foregroundColor(Color("second"))
.debugFrame()
.debugFrame(.size)
.padding(.trailing, 25.0)
}
HStack {
RoundedRectangle(cornerRadius: 4.0)
.frame(width: 120.0, height: 100.0)
.foregroundColor(Color("third"))
.debugFrame()
.debugFrame(color: .white, .originX, .width)
.padding(.leading, 20.0)
Spacer()
}
Expand Down
3 changes: 2 additions & 1 deletion Example/Example/ExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// ExampleApp.swift
// Example
//
// Created by Vlad Shkodich on 19.12.2022.
// Created by Vladyslav Shkodych on 19.12.2022.
//

import SwiftUI
import DebugFrame

@main
struct ExampleApp: App {

var body: some Scene {
WindowGroup {
ContentView()
Expand Down
2 changes: 1 addition & 1 deletion Example/ExampleTests/ContentViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ExampleTests.swift
// ExampleTests
//
// Created by Vlad Shkodich on 19.12.2022.
// Created by Vladyslav Shkodych on 19.12.2022.
//

import XCTest
Expand Down
2 changes: 1 addition & 1 deletion Example/ExampleTests/ViewImageConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ViewImageConfig.swift
// ExampleTests
//
// Created by Vlad Shkodich on 19.12.2022.
// Created by Vladyslav Shkodych on 19.12.2022.
//

import Foundation
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use_frameworks!
install! 'cocoapods', :warn_for_unused_master_specs_repo => false

target 'Example' do
pod 'DebugFrame'

pod 'DebugFrame', :path => '../'
end

target 'ExampleTests' do

# SnapshotTesting; https://github.com/pointfreeco/swift-snapshot-testing
pod 'SnapshotTesting', '1.9.0', :inhibit_warnings => true
end
Expand Down
13 changes: 8 additions & 5 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
PODS:
- DebugFrame (0.0.1)
- DebugFrame (0.1.3)
- SnapshotTesting (1.9.0)

DEPENDENCIES:
- DebugFrame
- DebugFrame (from `../`)
- SnapshotTesting (= 1.9.0)

SPEC REPOS:
trunk:
- DebugFrame
- SnapshotTesting

EXTERNAL SOURCES:
DebugFrame:
:path: "../"

SPEC CHECKSUMS:
DebugFrame: 39f20e1bbe6150058e239b744cccf0c8bf649f86
DebugFrame: e563c37cd7499f172aad51c44fc2034f51a6544e
SnapshotTesting: 6141c48b6aa76ead61431ca665c14ab9a066c53b

PODFILE CHECKSUM: 6e25a6dad0d57076d1de03bce513a768e5ed8d79
PODFILE CHECKSUM: 8f2d82fe448e2e09320a9a650ed45b374d09e3ba

COCOAPODS: 1.11.3
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ DebugFrame is a modifier for SwiftUI view, which is very easy to use and add to
```
VStack {
title
.debugFrame()
Spacer()
rectangles
.debugFrame()
.debugFrame(color: .black)
Spacer()
}
.debugFrame()
.debugFrame(color: .black, .size)
```

<img width="436" alt="Screenshot 2022-12-19 at 19 08 31" src="https://user-images.githubusercontent.com/43936569/208482070-81ababbe-250f-455a-9e1e-b0444aa2c86c.png">
<img width="436" alt="Screenshot 2023-04-06 at 03 06 00" src="https://user-images.githubusercontent.com/43936569/230239666-4d91d59b-84a9-4a49-9db4-e2f5d8168470.png">
2 changes: 1 addition & 1 deletion Source/DebugFrame/DebugFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// DebugFrame.h
// DebugFrame
//
// Created by Vlad Shkodich on 19.12.2022.
// Created by Vladyslav Shkodych on 19.12.2022.
//

#import <Foundation/Foundation.h>
Expand Down
80 changes: 71 additions & 9 deletions Source/DebugFrame/DebugFrame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@
// DebugFrame.swift
// DebugFrame
//
// Created by Vlad Shkodich on 19.12.2022.
// Created by Vladyslav Shkodych on 19.12.2022.
//

/*
DebugFrame is an overlay that can be appended to your view, for debugging its origin and size.

DebugFrame will not be compiled in the release build, so you may not worry if you forget to remove it from some of your view.
To keep your code as clean as possible, it's probably a good idea to add a SwiftLint or any other linting rule to make an Xcode warning for this.

There are two methods for modifying your view with debugFrame overlay.
The two methods need only for easier use of the Xcode autocomplete.
*/

import Foundation
import SwiftUI

private struct DebugFrame: ViewModifier {

let color: Color
let outputs: [DebugFrameOutput]

func body(content: Content) -> some View {
#if !DEBUG
return content
Expand All @@ -18,29 +31,78 @@ private struct DebugFrame: ViewModifier {
content
.overlay(
GeometryReader { geometry in
let globalOrigin: CGPoint = geometry.frame(in: .global).origin
let origin: String = "(x: \(rounded(globalOrigin.x)), y: \(rounded(globalOrigin.y)))"
let size: String = "(w: \(rounded(geometry.size.width)), h: \(rounded(geometry.size.height)))"
ZStack(alignment: .bottom) {
Rectangle()
.strokeBorder(style: StrokeStyle(lineWidth: 1, dash: [5]))
.foregroundColor(Color.red)
Text("\(origin) | \(size)")
.foregroundColor(Color.red)
.foregroundColor(color)
Text(defineOutput(with: geometry))
.foregroundColor(color)
.font(.caption2)
}
}
)
}

private func defineOutput(with geometry: GeometryProxy) -> String {
let globalOrigin: CGPoint = geometry.frame(in: .global).origin
let originX: String = "x: \(rounded(globalOrigin.x))"
let originY: String = "y: \(rounded(globalOrigin.y))"
let width: String = "w: \(rounded(geometry.size.width))"
let height: String = "h: \(rounded(geometry.size.height))"
return String(
outputs.reduce(into: String()) {
switch $1 {
case .all: $0 += "(\(originX), \(originY)) | (\(width), \(height))"
case .origin: $0 += "(\(originX), \(originY))"
case .size: $0 += "(\(width), \(height))"
case .originX: $0 += originX
case .originY: $0 += originY
case .width: $0 += width
case .height: $0 += height
}
$0 += " | "
}.dropLast(3)
)
}

private func rounded(_ value: CGFloat) -> Float {
return Float(round(100 * value) / 100)
}
}

public enum DebugFrameOutput {

case all
case origin, size
case originX, originY
case width, height
}

public extension View {

func debugFrame() -> some View {
return modifier(DebugFrame())
/// Applies an overlay to your view and returns a new view. The overlay contains a border of your view, its origin, and size.
/// - Parameter color: Default color for the border of the overlay and outputs.
/// - Returns: View that uses the debug overlay.
func debugFrame(color: Color = Color.red) -> some View {
return modifier(
DebugFrame(
color: color,
outputs: [DebugFrameOutput.all]
)
)
}

/// Applies an overlay to your view and returns a new view. The overlay contains a border of your view, its origin, and size.
/// - Parameters:
/// - color: Default color for the border of the overlay and outputs.
/// - outputs: Default outputs of the overlay such as origin and size.
/// - Returns: View that uses the debug overlay.
func debugFrame(color: Color = Color.red, _ outputs: DebugFrameOutput...) -> some View {
return modifier(
DebugFrame(
color: color,
outputs: outputs.isEmpty ? [DebugFrameOutput.all] : outputs
)
)
}
}

0 comments on commit b08cabf

Please sign in to comment.