Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to use custom texts #25

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Demo/SwiftyCropDemo/SwiftyCropDemoApp.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// SwiftyCropDemoApp.swift
// SwiftyCropDemo
//
// Created by Leonid Zolotarev on 1/23/24.
//

import SwiftUI

@main
Expand Down
7 changes: 0 additions & 7 deletions Demo/SwiftyCropDemo/UIElements/LongText.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// LongText.swift
// SwiftyCropDemo
//
// Created by Leonid Zolotarev on 1/24/24.
//

import SwiftUI

struct LongText: View {
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ You can also configure `SwiftyCropView` by passing a `SwiftyCropConfiguration`.
| `rotateImage` | `Bool`: Whether the image can be rotated when cropping using pinch gestures. Defaults to `false`. |
| `zoomSensitivity` | `CGFloat`: Zoom sensitivity when cropping. Increase to make zoom faster / less sensitive. Defaults to `1.0`. |
| `rectAspectRatio` | `CGFloat`: The aspect ratio to use when a rectangular mask shape is used. Defaults to `4:3`. |
| `customTexts` | `Texts`: Defines custom texts for the buttons and instructions. Defaults to `nil` using localized strings from resources. |

Create a configuration like this:
```swift
Expand All @@ -164,7 +165,12 @@ let configuration = SwiftyCropConfiguration(
cropImageCircular: false,
rotateImage: true,
zoomSensitivity = 1.0,
rectAspectRatio = 4/3
rectAspectRatio = 4/3,
customTexts = SwiftyCropConfiguration.Texts(
cancelButtonText: "Cancel",
interactionInstructionsText: "Custom instruction text",
saveButtonText: "Save"
)
)
```
and use it like this:
Expand Down Expand Up @@ -201,7 +207,7 @@ Thanks to [@yhirano](https://github.com/yhirano) for adding the japanese localiz

## ✍️ Author

Benedikt Betz & CHECK24
Benedikt Betz

## 📃 License

Expand Down
23 changes: 22 additions & 1 deletion Sources/SwiftyCrop/Models/SwiftyCropConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ public struct SwiftyCropConfiguration {
public let rotateImage: Bool
public let zoomSensitivity: CGFloat
public let rectAspectRatio: CGFloat
public let customTexts: Texts?

public struct Texts {
public init(
cancelButtonText: String,
interactionInstructionsText: String,
saveButtonText: String
) {
self.cancelButtonText = cancelButtonText
self.interactionInstructionsText = interactionInstructionsText
self.saveButtonText = saveButtonText
}

public let cancelButtonText: String
public let interactionInstructionsText: String
public let saveButtonText: String
}

/// Creates a new instance of `SwiftyCropConfiguration`.
///
Expand All @@ -23,19 +40,23 @@ public struct SwiftyCropConfiguration {
/// - zoomSensitivity: Sensitivity when zooming. Default is `1.0`. Decrease to increase sensitivity.
///
/// - rectAspectRatio: The aspect ratio to use when a `.rectangle` mask shape is used. Defaults to `4:3`.
///
/// - customTexts: `Texts` object when using custom texts for the cropping view.
public init(
maxMagnificationScale: CGFloat = 4.0,
maskRadius: CGFloat = 130,
cropImageCircular: Bool = false,
rotateImage: Bool = false,
zoomSensitivity: CGFloat = 1,
rectAspectRatio: CGFloat = 4/3
rectAspectRatio: CGFloat = 4/3,
customTexts: Texts? = nil
) {
self.maxMagnificationScale = maxMagnificationScale
self.maskRadius = maskRadius
self.cropImageCircular = cropImageCircular
self.rotateImage = rotateImage
self.zoomSensitivity = zoomSensitivity
self.rectAspectRatio = rectAspectRatio
self.customTexts = customTexts
}
}
15 changes: 12 additions & 3 deletions Sources/SwiftyCrop/View/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ struct CropView: View {
}

VStack {
Text("interaction_instructions", tableName: localizableTableName, bundle: .module)
Text(
configuration.customTexts?.interactionInstructionsText ??
NSLocalizedString("interaction_instructions", tableName: localizableTableName, bundle: .module, comment: "")
)
.font(.system(size: 16, weight: .regular))
.foregroundColor(.white)
.padding(.top, 30)
Expand Down Expand Up @@ -116,7 +119,10 @@ struct CropView: View {
Button {
dismiss()
} label: {
Text("cancel_button", tableName: localizableTableName, bundle: .module)
Text(
configuration.customTexts?.cancelButtonText ??
NSLocalizedString("cancel_button", tableName: localizableTableName, bundle: .module, comment: "")
)
}
.foregroundColor(.white)

Expand All @@ -126,7 +132,10 @@ struct CropView: View {
onComplete(cropImage())
dismiss()
} label: {
Text("save_button", tableName: localizableTableName, bundle: .module)
Text(
configuration.customTexts?.saveButtonText ??
NSLocalizedString("save_button", tableName: localizableTableName, bundle: .module, comment: "")
)
}
.foregroundColor(.white)
}
Expand Down
10 changes: 9 additions & 1 deletion Tests/SwiftyCropTests/SwiftyCropTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ final class SwiftyCropTests: XCTestCase {
maxMagnificationScale: 1.0,
maskRadius: 1.0,
cropImageCircular: true,
rectAspectRatio: 4/3
rectAspectRatio: 4/3,
customTexts: SwiftyCropConfiguration.Texts(
cancelButtonText: "Test 1",
interactionInstructionsText: "Test 2",
saveButtonText: "Test 3"
)
)

XCTAssertEqual(configuration.maxMagnificationScale, 1.0)
XCTAssertEqual(configuration.maskRadius, 1.0)
XCTAssertEqual(configuration.cropImageCircular, true)
XCTAssertEqual(configuration.rectAspectRatio, 4/3)
XCTAssertEqual(configuration.customTexts?.cancelButtonText, "Test 1")
XCTAssertEqual(configuration.customTexts?.interactionInstructionsText, "Test 2")
XCTAssertEqual(configuration.customTexts?.saveButtonText, "Test 3")
}
}
Loading