Skip to content

Commit

Permalink
Adjusted configuration for texts, fonts and colors
Browse files Browse the repository at this point in the history
  • Loading branch information
benedom committed Oct 21, 2024
1 parent 22ed64a commit c740e4e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 46 deletions.
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,34 @@ 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. |
| `texts` | `Texts`: Defines custom texts for the buttons and instructions. Defaults to using localized strings from resources. |
| `fonts` | `Fonts`: Defines custom fonts for the buttons and instructions. Defaults to using system font. |
| `colors` | `Colors`: Defines custom colors for the texts and background. Defaults to white text and black background. |

Create a configuration like this:
```swift
let configuration = SwiftyCropConfiguration(
maxMagnificationScale = 4.0,
maxMagnificationScale: 4.0,
maskRadius: 130,
cropImageCircular: false,
rotateImage: true,
zoomSensitivity = 1.0,
rectAspectRatio = 4/3,
customTexts = SwiftyCropConfiguration.Texts(
cancelButtonText: "Cancel",
interactionInstructionsText: "Custom instruction text",
saveButtonText: "Save"
zoomSensitivity: 1.0,
rectAspectRatio: 4/3,
texts: SwiftyCropConfiguration.Texts(
cancelButton: "Cancel",
interactionInstructions: "Custom instruction text",
saveButton: "Save"
),
fonts: SwiftyCropConfiguration.Fonts(
cancelButton: Font.system(size: 12),
interactionInstructions: Font.system(size: 14),
saveButton: Font.system(size: 12)
),
colors: SwiftyCropConfiguration.Colors(
cancelButton: Color.red,
interactionInstructions: Color.white,
saveButton: Color.blue,
background: Color.gray
)
)
```
Expand Down Expand Up @@ -205,6 +218,8 @@ Thanks to [@insub](https://github.com/insub4067) for adding the korean localizat

Thanks to [@yhirano](https://github.com/yhirano) for adding the japanese localization 🇯🇵

Thanks to [@yefimtsev](https://github.com/yefimtsev) for adding the ability to customize fonts and colors 🖼️

## ✍️ Author

Benedikt Betz
Expand Down
67 changes: 36 additions & 31 deletions Sources/SwiftyCrop/Models/SwiftyCropConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,43 @@ public struct SwiftyCropConfiguration {
public let rotateImage: Bool
public let zoomSensitivity: CGFloat
public let rectAspectRatio: CGFloat
public let customTexts: Texts?
public let texts: Texts
public let fonts: Fonts
public let colors: Colors

public struct Texts {
public init(
cancelButtonText: String,
interactionInstructionsText: String,
saveButtonText: String
// We cannot use the localized values here because module access is not given in init
cancelButton: String? = nil,
interactionInstructions: String? = nil,
saveButton: String? = nil
) {
self.cancelButtonText = cancelButtonText
self.interactionInstructionsText = interactionInstructionsText
self.saveButtonText = saveButtonText
self.cancelButton = cancelButton
self.interactionInstructions = interactionInstructions
self.saveButton = saveButton
}

public let cancelButtonText: String
public let interactionInstructionsText: String
public let saveButtonText: String
public let cancelButton: String?
public let interactionInstructions: String?
public let saveButton: String?
}

public struct Fonts {
public init(
cancelButton: Font? = nil,
interactionInstructions: Font? = nil,
saveButton: Font? = nil
) {
self.cancelButton = cancelButton
self.interactionInstructions = interactionInstructions ?? .system(size: 16, weight: .regular)
self.saveButton = saveButton
}

public let cancelButton: Font?
public let interactionInstructions: Font
public let saveButton: Font?
}

public struct Colors {
public init(
cancelButton: Color = .white,
Expand All @@ -48,22 +65,6 @@ public struct SwiftyCropConfiguration {
public let background: Color
}

public struct Fonts {
public init(
cancelButton: Font? = nil,
interactionInstructions: Font? = nil,
saveButton: Font? = nil
) {
self.cancelButton = cancelButton
self.interactionInstructions = interactionInstructions ?? .system(size: 16, weight: .regular)
self.saveButton = saveButton
}

public let cancelButton: Font?
public let interactionInstructions: Font
public let saveButton: Font?
}

/// Creates a new instance of `SwiftyCropConfiguration`.
///
/// - Parameters:
Expand All @@ -79,25 +80,29 @@ public struct SwiftyCropConfiguration {
///
/// - 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.
/// - texts: `Texts` object when using custom texts for the cropping view.
///
/// - fonts: `Fonts` object when using custom fonts for the cropping view. Defaults to system.
///
/// - colors: `Colors` object when using custom colors for the cropping view. Defaults to white text and black background.
public init(
maxMagnificationScale: CGFloat = 4.0,
maskRadius: CGFloat = 130,
cropImageCircular: Bool = false,
rotateImage: Bool = false,
zoomSensitivity: CGFloat = 1,
rectAspectRatio: CGFloat = 4/3,
customTexts: Texts? = nil,
fonts: Fonts = .init(),
colors: Colors = .init()
texts: Texts = Texts(),
fonts: Fonts = Fonts(),
colors: Colors = Colors()
) {
self.maxMagnificationScale = maxMagnificationScale
self.maskRadius = maskRadius
self.cropImageCircular = cropImageCircular
self.rotateImage = rotateImage
self.zoomSensitivity = zoomSensitivity
self.rectAspectRatio = rectAspectRatio
self.customTexts = customTexts
self.texts = texts
self.fonts = fonts
self.colors = colors
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftyCrop/View/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct CropView: View {

VStack {
Text(
configuration.customTexts?.interactionInstructionsText ??
configuration.texts.interactionInstructions ??
NSLocalizedString("interaction_instructions", tableName: localizableTableName, bundle: .module, comment: "")
)
.font(configuration.fonts.interactionInstructions)
Expand Down Expand Up @@ -120,7 +120,7 @@ struct CropView: View {
dismiss()
} label: {
Text(
configuration.customTexts?.cancelButtonText ??
configuration.texts.cancelButton ??
NSLocalizedString("cancel_button", tableName: localizableTableName, bundle: .module, comment: "")
)
}
Expand All @@ -134,7 +134,7 @@ struct CropView: View {
dismiss()
} label: {
Text(
configuration.customTexts?.saveButtonText ??
configuration.texts.saveButton ??
NSLocalizedString("save_button", tableName: localizableTableName, bundle: .module, comment: "")
)
.font(configuration.fonts.saveButton)
Expand Down
29 changes: 25 additions & 4 deletions Tests/SwiftyCropTests/SwiftyCropTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,40 @@ final class SwiftyCropTests: XCTestCase {
maskRadius: 1.0,
cropImageCircular: true,
rectAspectRatio: 4/3,
customTexts: SwiftyCropConfiguration.Texts(
texts: SwiftyCropConfiguration.Texts(
cancelButtonText: "Test 1",
interactionInstructionsText: "Test 2",
saveButtonText: "Test 3"
),
fonts: SwiftyCropConfiguration.Fonts(
cancelButton: Font.system(size: 12),
interactionInstructions: .systemFont(ofSize: 13),
saveButton: .systemFont(ofSize: 14)
),
colors: SwiftyCropConfiguration.Colors(
cancelButton: .red,
interactionInstructions: .yellow,
saveButton: .green,
background: .gray
)
)

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")

XCTAssertEqual(configuration.texts.cancelButton, "Test 1")
XCTAssertEqual(configuration.texts.interactionInstructions, "Test 2")
XCTAssertEqual(configuration.texts.saveButton, "Test 3")

XCTAssertEqual(configuration.fonts.cancelButton, Font.system(size: 12))
XCTAssertEqual(configuration.fonts.interactionInstructions, Font.system(size: 13))
XCTAssertEqual(configuration.fonts.saveButton, Font.system(size: 14))

XCTAssertEqual(configuration.colors.cancelButton, Color.red)
XCTAssertEqual(configuration.colors.interactionInstructions, Color.yellow)
XCTAssertEqual(configuration.colors.saveButton, Color.green)
XCTAssertEqual(configuration.colors.background, Color.gray)
}
}

0 comments on commit c740e4e

Please sign in to comment.