|
@@ -7,19 +7,22 @@ struct DecimalTextField: UIViewRepresentable {
|
|
|
private var formatter: NumberFormatter
|
|
private var formatter: NumberFormatter
|
|
|
private var autofocus: Bool
|
|
private var autofocus: Bool
|
|
|
private var cleanInput: Bool
|
|
private var cleanInput: Bool
|
|
|
|
|
+ private var useButtons: Bool
|
|
|
|
|
|
|
|
init(
|
|
init(
|
|
|
_ placeholder: String,
|
|
_ placeholder: String,
|
|
|
value: Binding<Decimal>,
|
|
value: Binding<Decimal>,
|
|
|
formatter: NumberFormatter,
|
|
formatter: NumberFormatter,
|
|
|
autofocus: Bool = false,
|
|
autofocus: Bool = false,
|
|
|
- cleanInput: Bool = false
|
|
|
|
|
|
|
+ cleanInput: Bool = false,
|
|
|
|
|
+ useButtons: Bool = true
|
|
|
) {
|
|
) {
|
|
|
self.placeholder = placeholder
|
|
self.placeholder = placeholder
|
|
|
_value = value
|
|
_value = value
|
|
|
self.formatter = formatter
|
|
self.formatter = formatter
|
|
|
self.autofocus = autofocus
|
|
self.autofocus = autofocus
|
|
|
self.cleanInput = cleanInput
|
|
self.cleanInput = cleanInput
|
|
|
|
|
+ self.useButtons = useButtons
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func makeUIView(context: Context) -> UITextField {
|
|
func makeUIView(context: Context) -> UITextField {
|
|
@@ -30,6 +33,35 @@ struct DecimalTextField: UIViewRepresentable {
|
|
|
textfield.text = cleanInput ? "" : formatter.string(for: value) ?? placeholder
|
|
textfield.text = cleanInput ? "" : formatter.string(for: value) ?? placeholder
|
|
|
textfield.textAlignment = .right
|
|
textfield.textAlignment = .right
|
|
|
|
|
|
|
|
|
|
+ lazy var toolBar: UIToolbar = {
|
|
|
|
|
+ let tool: UIToolbar = .init(frame: .init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 35))
|
|
|
|
|
+ tool.barStyle = .default
|
|
|
|
|
+ tool.isTranslucent = true
|
|
|
|
|
+ tool.sizeToFit()
|
|
|
|
|
+
|
|
|
|
|
+ let spaceArea: UIBarButtonItem = .init(systemItem: .flexibleSpace)
|
|
|
|
|
+ let clearButton: UIBarButtonItem = .init(
|
|
|
|
|
+ title: "Clear",
|
|
|
|
|
+ style: .plain,
|
|
|
|
|
+ target: self,
|
|
|
|
|
+ action: #selector(textfield.clearButtonTapped(button:))
|
|
|
|
|
+ )
|
|
|
|
|
+ let doneButton: UIBarButtonItem = .init(
|
|
|
|
|
+ title: "Done",
|
|
|
|
|
+ style: .done,
|
|
|
|
|
+ target: self,
|
|
|
|
|
+ action: #selector(textfield.doneButtonTapped(button:))
|
|
|
|
|
+ )
|
|
|
|
|
+ tool.setItems([clearButton, spaceArea, doneButton], animated: false)
|
|
|
|
|
+ tool.isUserInteractionEnabled = true
|
|
|
|
|
+
|
|
|
|
|
+ return tool
|
|
|
|
|
+ }()
|
|
|
|
|
+
|
|
|
|
|
+ if useButtons {
|
|
|
|
|
+ textfield.inputAccessoryView = toolBar
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if autofocus {
|
|
if autofocus {
|
|
|
DispatchQueue.main.async {
|
|
DispatchQueue.main.async {
|
|
|
textfield.becomeFirstResponder()
|
|
textfield.becomeFirstResponder()
|