Skip to content

Commit

Permalink
feat: Customize crop toolbar color (#178)
Browse files Browse the repository at this point in the history
* refactor: Separate config, enum and typealias from Mantis.swift

Add CropToolbarConfig to CropToolbarProtocol

* refactor: refactor CropToolbarProtocol and CropToolbarConfig

extract Config, Definition and Enum from  Mantis.swift
fix some typos
  • Loading branch information
guoyingtao committed Jul 12, 2022
1 parent 33c18a5 commit eaa8f06
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 243 deletions.
31 changes: 20 additions & 11 deletions Example/CustomizedCropToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,27 @@ import UIKit
import Mantis

class CustomizedCropToolbar: UIView, CropToolbarProtocol {
var heightForVerticalOrientation: CGFloat?

var widthForHorizonOrientation: CGFloat?

var iconProvider: CropToolbarIconProvider?

weak var cropToolbarDelegate: CropToolbarDelegate?

private (set)var config: CropToolbarConfigProtocol?

private var fixedRatioSettingButton: UIButton?
private var cropButton: UIButton?
private var cancelButton: UIButton?
private var stackView: UIStackView?
private var config: CropToolbarConfig!

var custom: ((Double) -> Void)?

func createToolbarUI(config: CropToolbarConfig) {
func createToolbarUI(config: CropToolbarConfigProtocol?) {
self.config = config

backgroundColor = .red
guard let config = config else {
return
}

backgroundColor = config.backgroundColor

cropButton = createOptionButton(withTitle: "Crop", andAction: #selector(crop))
cancelButton = createOptionButton(withTitle: "Cancel", andAction: #selector(cancel))
Expand Down Expand Up @@ -71,10 +72,14 @@ class CustomizedCropToolbar: UIView, CropToolbarProtocol {

public override var intrinsicContentSize: CGSize {
let superSize = super.intrinsicContentSize
guard let config = config else {
return superSize
}

if Orientation.isPortrait {
return CGSize(width: superSize.width, height: heightForVerticalOrientation ?? 44)
return CGSize(width: superSize.width, height: config.heightForVerticalOrientation)
} else {
return CGSize(width: widthForHorizonOrientation ?? 44, height: superSize.height)
return CGSize(width: config.widthForHorizontalOrientation, height: superSize.height)
}
}

Expand All @@ -95,15 +100,19 @@ class CustomizedCropToolbar: UIView, CropToolbarProtocol {
}

private func createOptionButton(withTitle title: String?, andAction action: Selector) -> UIButton {
let buttonColor = UIColor.white
guard let config = config else {
return UIButton()
}

let buttonColor = config.foregroundColor
let buttonFontSize: CGFloat = (UIDevice.current.userInterfaceIdiom == .pad) ?
config.optionButtonFontSizeForPad :
config.optionButtonFontSize

let buttonFont = UIFont.systemFont(ofSize: buttonFontSize)

let button = UIButton(type: .system)
button.tintColor = .white
button.tintColor = config.foregroundColor
button.titleLabel?.font = buttonFont

if let title = title {
Expand Down
26 changes: 15 additions & 11 deletions Example/CustomizedCropToolbarWithoutList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@ import UIKit
import Mantis

class CustomizedCropToolbarWithoutList: UIView, CropToolbarProtocol {
var heightForVerticalOrientation: CGFloat?

var widthForHorizonOrientation: CGFloat?

var iconProvider: CropToolbarIconProvider?
var iconProvider: CropToolbarIconProvider?
weak var cropToolbarDelegate: CropToolbarDelegate?

private (set)var config: CropToolbarConfigProtocol?

private var fixedRatioSettingButton: UIButton?
private var portraitRatioButton: UIButton?
private var landscapeRatioButton: UIButton?
private var cropButton: UIButton?
private var cancelButton: UIButton?
private var stackView: UIStackView?
private var config: CropToolbarConfig!


var custom: ((Double) -> Void)?

func createToolbarUI(config: CropToolbarConfig) {
func createToolbarUI(config: CropToolbarConfigProtocol?) {
self.config = config

backgroundColor = .darkGray
Expand Down Expand Up @@ -56,7 +53,6 @@ class CustomizedCropToolbarWithoutList: UIView, CropToolbarProtocol {
}

public func handleFixedRatioSetted(ratio: Double) {

switch ratio {
case 9 / 16:
portraitRatioButton?.setTitleColor(.blue, for: .normal)
Expand All @@ -76,10 +72,14 @@ class CustomizedCropToolbarWithoutList: UIView, CropToolbarProtocol {

public override var intrinsicContentSize: CGSize {
let superSize = super.intrinsicContentSize
guard let config = config else {
return superSize
}

if Orientation.isPortrait {
return CGSize(width: superSize.width, height: heightForVerticalOrientation ?? 44)
return CGSize(width: superSize.width, height: config.heightForVerticalOrientation)
} else {
return CGSize(width: widthForHorizonOrientation ?? 44, height: superSize.height)
return CGSize(width: config.widthForHorizontalOrientation, height: superSize.height)
}
}

Expand Down Expand Up @@ -108,6 +108,10 @@ class CustomizedCropToolbarWithoutList: UIView, CropToolbarProtocol {
}

private func createOptionButton(withTitle title: String?, andAction action: Selector) -> UIButton {
guard let config = config else {
return UIButton()
}

let buttonColor = UIColor.white
let buttonFontSize: CGFloat = (UIDevice.current.userInterfaceIdiom == .pad) ?
config.optionButtonFontSizeForPad :
Expand Down
18 changes: 10 additions & 8 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,14 @@ class ViewController: UIViewController, CropViewControllerDelegate {
present(cropViewController, animated: true)
}


@IBAction func customizedCropToolbarButtonTouched(_ sender: Any) {
guard let image = image else {
return
}
var config = Mantis.Config()

config.cropToolbarConfig.cropToolbarHeightForVertialOrientation = 100
config.cropToolbarConfig.cropToolbarWidthForHorizontalOrientation = 80
var config = Mantis.Config()
config.cropToolbarConfig = CropToolbarConfig()
config.cropToolbarConfig.backgroundColor = .red
config.cropToolbarConfig.foregroundColor = .white

let cropToolbar = CustomizedCropToolbar(frame: .zero)

Expand All @@ -122,8 +121,8 @@ class ViewController: UIViewController, CropViewControllerDelegate {
}
var config = Mantis.Config()

config.cropToolbarConfig.cropToolbarHeightForVertialOrientation = 160
config.cropToolbarConfig.cropToolbarWidthForHorizontalOrientation = 80
config.cropToolbarConfig.heightForVerticalOrientation = 160
config.cropToolbarConfig.widthForHorizontalOrientation = 80

let cropToolbar = CustomizedCropToolbarWithoutList(frame: .zero)

Expand All @@ -142,7 +141,10 @@ class ViewController: UIViewController, CropViewControllerDelegate {

var config = Mantis.Config()
config.cropToolbarConfig.toolbarButtonOptions = [.clockwiseRotate, .reset, .ratio, .alterCropper90Degree]

config.cropToolbarConfig.backgroundColor = .white
config.cropToolbarConfig.foregroundColor = .gray
config.cropToolbarConfig.ratioCandidatesShowType = .alwaysShowRatioList

let cropViewController = Mantis.cropViewController(image: image,
config: config)
cropViewController.modalPresentationStyle = .fullScreen
Expand Down
2 changes: 1 addition & 1 deletion Mantis.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Pod::Spec.new do |s|
s.social_media_url = "http://twitter.com/guoyingtao"
s.platform = :ios
s.swift_version = "5.0"
s.ios.deployment_target = "11.0"
s.ios.deployment_target = "13.0"
s.source = { :git => "https://github.com/guoyingtao/Mantis.git", :tag => "v#{s.version}" }
s.source_files = "Sources/**/*.{h,swift}"
s.resource_bundles = {
Expand Down
20 changes: 16 additions & 4 deletions Mantis.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
/* Begin PBXBuildFile section */
269AF94227437EE400F7FAF6 /* DialConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269AF94127437EE400F7FAF6 /* DialConfig.swift */; };
5F17E40A253535F300A3EB7D /* Orientation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F17E409253535F300A3EB7D /* Orientation.swift */; };
5F69CC5E26C0629400568B75 /* definitions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F69CC5D26C0629400568B75 /* definitions.swift */; };
5F69CC5E26C0629400568B75 /* Definition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F69CC5D26C0629400568B75 /* Definition.swift */; };
5F7D22AE245BCA8D0015A0D5 /* CropToolbarProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F7D22AD245BCA8D0015A0D5 /* CropToolbarProtocol.swift */; };
5FCE938724834C57002BBE65 /* ToolbarButtonOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FCE938624834C57002BBE65 /* ToolbarButtonOptions.swift */; };
6659429D2877DAE80096A5DB /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6659429C2877DAE80096A5DB /* Config.swift */; };
6659429F2877DC8A0096A5DB /* Enum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6659429E2877DC8A0096A5DB /* Enum.swift */; };
665942A12877DD1C0096A5DB /* TypeAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = 665942A02877DD1C0096A5DB /* TypeAlias.swift */; };
8EFB6F6725D16B0F00C0DDB2 /* MantisLocalizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 8EFB6F3525D154D900C0DDB2 /* MantisLocalizable.strings */; };
8EFB6FB125D187A000C0DDB2 /* Resource.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 8EFB6F5425D16A9E00C0DDB2 /* Resource.bundle */; };
FEDAAD8625205CC300D95667 /* RatioSelector.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEDAAD8525205CC300D95667 /* RatioSelector.swift */; };
Expand Down Expand Up @@ -95,9 +98,12 @@
/* Begin PBXFileReference section */
269AF94127437EE400F7FAF6 /* DialConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DialConfig.swift; sourceTree = "<group>"; };
5F17E409253535F300A3EB7D /* Orientation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Orientation.swift; sourceTree = "<group>"; };
5F69CC5D26C0629400568B75 /* definitions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = definitions.swift; sourceTree = "<group>"; };
5F69CC5D26C0629400568B75 /* Definition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Definition.swift; sourceTree = "<group>"; };
5F7D22AD245BCA8D0015A0D5 /* CropToolbarProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CropToolbarProtocol.swift; sourceTree = "<group>"; };
5FCE938624834C57002BBE65 /* ToolbarButtonOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToolbarButtonOptions.swift; sourceTree = "<group>"; };
6659429C2877DAE80096A5DB /* Config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
6659429E2877DC8A0096A5DB /* Enum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Enum.swift; sourceTree = "<group>"; };
665942A02877DD1C0096A5DB /* TypeAlias.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypeAlias.swift; sourceTree = "<group>"; };
8EFB6F3625D154D900C0DDB2 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/MantisLocalizable.strings; sourceTree = "<group>"; };
8EFB6F3725D154D900C0DDB2 /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/MantisLocalizable.strings; sourceTree = "<group>"; };
8EFB6F3825D154D900C0DDB2 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/MantisLocalizable.strings"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -330,9 +336,12 @@
children = (
OBJ_9 /* Mantis.h */,
OBJ_36 /* Mantis.swift */,
6659429C2877DAE80096A5DB /* Config.swift */,
6659429E2877DC8A0096A5DB /* Enum.swift */,
665942A02877DD1C0096A5DB /* TypeAlias.swift */,
OBJ_41 /* RatioOptions.swift */,
5FCE938624834C57002BBE65 /* ToolbarButtonOptions.swift */,
5F69CC5D26C0629400568B75 /* definitions.swift */,
5F69CC5D26C0629400568B75 /* Definition.swift */,
OBJ_10 /* Info.plist */,
OBJ_11 /* CropView */,
OBJ_23 /* CropViewController */,
Expand Down Expand Up @@ -543,6 +552,7 @@
OBJ_75 /* CropView+UIScrollViewDelegate.swift in Sources */,
OBJ_76 /* CropView.swift in Sources */,
OBJ_77 /* CropViewModel.swift in Sources */,
6659429F2877DC8A0096A5DB /* Enum.swift in Sources */,
OBJ_78 /* CropViewStatus.swift in Sources */,
OBJ_79 /* ImageContainer.swift in Sources */,
5F7D22AE245BCA8D0015A0D5 /* CropToolbarProtocol.swift in Sources */,
Expand All @@ -554,7 +564,7 @@
5FCE938724834C57002BBE65 /* ToolbarButtonOptions.swift in Sources */,
OBJ_85 /* CGImageExtensions.swift in Sources */,
OBJ_86 /* CoreGraphicsExtensions.swift in Sources */,
5F69CC5E26C0629400568B75 /* definitions.swift in Sources */,
5F69CC5E26C0629400568B75 /* Definition.swift in Sources */,
OBJ_87 /* UIImageExtensions.swift in Sources */,
OBJ_88 /* GeometryHelper.swift in Sources */,
OBJ_89 /* LocalizedHelper.swift in Sources */,
Expand All @@ -563,6 +573,7 @@
OBJ_91 /* CropDimmingView.swift in Sources */,
OBJ_92 /* CropMaskProtocal.swift in Sources */,
FEDAAD8C25205DE900D95667 /* RatioItemView.swift in Sources */,
665942A12877DD1C0096A5DB /* TypeAlias.swift in Sources */,
FEDAAD8625205CC300D95667 /* RatioSelector.swift in Sources */,
OBJ_93 /* CropVisualEffectView.swift in Sources */,
269AF94227437EE400F7FAF6 /* DialConfig.swift in Sources */,
Expand All @@ -571,6 +582,7 @@
OBJ_97 /* RotationCalculator.swift in Sources */,
OBJ_98 /* RotationDial+Touches.swift in Sources */,
OBJ_99 /* RotationDial.swift in Sources */,
6659429D2877DAE80096A5DB /* Config.swift in Sources */,
OBJ_100 /* RotationDialPlate.swift in Sources */,
OBJ_101 /* RotationDialViewModel.swift in Sources */,
);
Expand Down

0 comments on commit eaa8f06

Please sign in to comment.