Skip to content

Commit 27240c7

Browse files
committed
add +/- button to DecimalButtonInputCell
1 parent e5dfc10 commit 27240c7

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

Sources/ObjectForm/view/DecimalButtonInputCell.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class DecimalButtonInputCell: FormInputCell {
2424
}
2525

2626
var numberLocale: Locale?
27+
private weak var alertController: UIAlertController?
2728

2829
private lazy var decimalButton: UIButton = {
2930
let button = UIButton(type: .system)
@@ -109,12 +110,19 @@ public class DecimalButtonInputCell: FormInputCell {
109110

110111
public func showDecimalInput(in viewController: UIViewController) {
111112
let alertController = UIAlertController(title: "", message: nil, preferredStyle: .alert)
113+
self.alertController = alertController
112114

113-
alertController.addTextField { textField in
115+
alertController.addTextField { [weak self] textField in
116+
guard let self = self else { return }
114117
textField.keyboardType = .decimalPad
115118
if let currentValue = self.outputValue {
116119
textField.text = self.numberFormatter.string(from: currentValue)
117120
}
121+
122+
// Add +/- button to keyboard toolbar
123+
let minusButton = KeyboardToolbarFactory.factoryKeyboardButton(title: "+/-")
124+
minusButton.addTarget(self, action: #selector(self.minusButtonTapped(_:)), for: .touchUpInside)
125+
textField.inputAccessoryView = KeyboardToolbarFactory.factoryKeyboardToolbar(leadingButtonList: [UIBarButtonItem(customView: minusButton)])
118126
}
119127

120128
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
@@ -142,4 +150,18 @@ public class DecimalButtonInputCell: FormInputCell {
142150

143151
viewController.present(alertController, animated: true)
144152
}
153+
154+
@objc private func minusButtonTapped(_ button: UIButton) {
155+
guard let textField = alertController?.textFields?.first,
156+
let text = textField.text else {
157+
return
158+
}
159+
160+
let token = "-"
161+
if text.hasPrefix(token) {
162+
textField.text = text.replacingOccurrences(of: token, with: "")
163+
} else {
164+
textField.text = token + text
165+
}
166+
}
145167
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import UIKit
2+
3+
struct KeyboardToolbarFactory {
4+
static func factoryKeyboardButton(title: String) -> UIButton {
5+
let button = UIButton(type: .custom)
6+
var config = UIButton.Configuration.bordered()
7+
config.attributedTitle = AttributedString(title, attributes: {
8+
var container = AttributeContainer()
9+
container.font = .systemFont(ofSize: 17.0, weight: .medium)
10+
return container
11+
}())
12+
config.baseForegroundColor = .label
13+
config.baseBackgroundColor = Self.keyboardToolbarButtonBackground
14+
button.layer.shadowOffset = CGSize(width: 0, height: 1)
15+
button.layer.shadowRadius = 1.0
16+
button.layer.shadowColor = UIColor.black.cgColor
17+
button.layer.shadowOpacity = 0.3
18+
button.configuration = config
19+
return button
20+
}
21+
22+
static var keyboardToolbarButtonBackground: UIColor {
23+
return UIColor { (traitCollection: UITraitCollection) -> UIColor in
24+
if traitCollection.userInterfaceStyle == .dark {
25+
return .systemGray2
26+
} else {
27+
return .systemBackground
28+
}
29+
}
30+
}
31+
32+
static func factoryKeyboardToolbar(leadingButtonList: [UIBarButtonItem], trailingButtonList: [UIBarButtonItem]? = nil) -> UIToolbar {
33+
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
34+
var buttonItemList = leadingButtonList
35+
buttonItemList.append(UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil))
36+
if let trailingButtonList {
37+
buttonItemList.append(contentsOf: trailingButtonList)
38+
}
39+
toolbar.setItems(buttonItemList, animated: true)
40+
return toolbar
41+
}
42+
}

0 commit comments

Comments
 (0)