Skip to content

Commit

Permalink
Merge pull request #25 from benedom/feature/add-custom-texts
Browse files Browse the repository at this point in the history
Added possibility to use custom texts
  • Loading branch information
benedom authored Oct 16, 2024
2 parents 57b4a32 + b5ea132 commit 8b6fc8b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
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")
}
}

0 comments on commit 8b6fc8b

Please sign in to comment.