-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* String.width * Text.foregroundLinearGradient * View.border * snapshotTestBorder --------- Co-authored-by: Oguz Yuksel <[email protected]>
- Loading branch information
1 parent
407b092
commit b3eb108
Showing
5 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
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
68 changes: 68 additions & 0 deletions
68
Sources/SnapshotTestingExtensions/Extensions/SwiftUI/View.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,68 @@ | ||
// Copyright © 2023 Lautsprecher Teufel GmbH. All rights reserved. | ||
|
||
#if DEBUG && canImport(SwiftUI) | ||
import SwiftUI | ||
import UIExtensions | ||
|
||
// MARK: - snapshotTestBorder | ||
extension View { | ||
/// This function allows you to add a custom border to a view for snapshot testing purposes. | ||
/// | ||
/// - Parameters: | ||
/// - title: A string that will be used to generate the repeated title inside the border. Defaults to "SnapshotTest". | ||
/// | ||
/// - Returns: A modified version of the view that has a custom border with a repeated title inside it. | ||
public func snapshotTestBorder(_ title: String = "SnapshotTest") -> some View { | ||
modifier(SnapshotTestBorderViewModifier(title)) | ||
} | ||
} | ||
|
||
struct SnapshotTestBorderViewModifier: ViewModifier { | ||
@State | ||
private var contentSize: CGSize = .zero | ||
private var fontSize: CGFloat { | ||
min(contentSize.width, contentSize.height) * 0.1 | ||
} | ||
private var repeatedTitle: String { | ||
var mutableRepeatedTitle = title | ||
while max(contentSize.height, contentSize.width) | ||
> mutableRepeatedTitle.width(for: .systemFont(ofSize: fontSize)) { | ||
mutableRepeatedTitle.append(title) | ||
} | ||
return mutableRepeatedTitle | ||
} | ||
private let title: String | ||
|
||
init(_ title: String) { | ||
self.title = title | ||
} | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.measureView { contentSize = $0 } | ||
.border( | ||
{ | ||
Text(repeatedTitle) | ||
.font(.system(size: fontSize)) | ||
.foregroundLinearGradient( | ||
[ | ||
.red, | ||
.orange, | ||
.yellow, | ||
.green, | ||
.init(red: 63, green: 0, blue: 255), | ||
.purple | ||
], | ||
startPoint: .topLeading, | ||
endPoint: .bottomTrailing | ||
) | ||
.lineLimit(1) | ||
.truncationMode(.middle) | ||
.multilineTextAlignment(.center) | ||
}, | ||
dividerColor: .red, | ||
dividerWidth: 0.4 | ||
) | ||
} | ||
} | ||
#endif |
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,29 @@ | ||
// Copyright © 2023 Lautsprecher Teufel GmbH. All rights reserved. | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
extension String { | ||
/** | ||
The extension provides a convenient way to calculate the width of a given string when rendered with a specific font. This is particularly useful for dynamically sizing UI elements such as labels or text fields based on their content. | ||
|
||
To use this extension, simply call the `width(for:)` method on any string instance and pass in the desired font as an argument. The method will return the calculated width of the string as a `CGFloat` value. | ||
|
||
Example Usage: | ||
|
||
let myString = "Hello, World!" | ||
let myFont = UIFont.systemFont(ofSize: 18) | ||
let stringWidth = myString.width(for: myFont) | ||
print(stringWidth) | ||
|
||
This will output the calculated width of the string "Hello, World!" when rendered with the system font at size 18. | ||
|
||
- Parameter font: The font to be used for rendering the string. | ||
- Returns: The width of the string when rendered with the given font. | ||
*/ | ||
public func width(for font: UIFont) -> CGFloat { | ||
let fontAttributes = [NSAttributedString.Key.font: font] | ||
let size = self.size(withAttributes: fontAttributes) | ||
return size.width | ||
} | ||
} | ||
#endif |
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,45 @@ | ||
// Copyright © 2023 Lautsprecher Teufel GmbH. All rights reserved. | ||
|
||
#if canImport(SwiftUI) | ||
import SwiftUI | ||
extension Text { | ||
/** | ||
The extension provides a way to apply a linear gradient as the foreground of a text view. This is useful for creating visually interesting text with gradients. | ||
|
||
To use this extension, call the `foregroundLinearGradient(colors:startPoint:endPoint:)` method on any Text view instance. Pass in an array of `Color` values to be used in the gradient, as well as the start and end points of the gradient, specified as `UnitPoint` values. | ||
|
||
Example Usage: | ||
|
||
Text("Hello, World!") | ||
.font(.title) | ||
.foregroundColor(.white) | ||
.foregroundLinearGradient( | ||
colors: [.red, .yellow], | ||
startPoint: .leading, | ||
endPoint: .trailing | ||
) | ||
|
||
This will apply a linear gradient from red to yellow as the foreground of the "Hello, World!" text. The gradient will start at the leading edge of the text and end at the trailing edge. | ||
|
||
- Parameters: | ||
- colors: An array of colors to be used in the gradient. | ||
- startPoint: The point at which the gradient begins. The point is specified as a unit coordinate space. | ||
- endPoint: The point at which the gradient ends. The point is specified as a unit coordinate space. | ||
- Returns: A view with a linear gradient as its foreground. | ||
*/ | ||
public func foregroundLinearGradient( | ||
_ colors: [Color], | ||
startPoint: UnitPoint, | ||
endPoint: UnitPoint | ||
) -> some View { | ||
self.overlay( | ||
LinearGradient( | ||
colors: colors, | ||
startPoint: startPoint, | ||
endPoint: endPoint | ||
) | ||
.mask(self) | ||
) | ||
} | ||
} | ||
#endif |
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