|
@@ -14,6 +14,7 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
|
|
|
var isDismissible: Bool
|
|
var isDismissible: Bool
|
|
|
var textFieldDidBeginEditing: (() -> Void)?
|
|
var textFieldDidBeginEditing: (() -> Void)?
|
|
|
var numberFormatter: NumberFormatter
|
|
var numberFormatter: NumberFormatter
|
|
|
|
|
+ var allowDecimalSeparator: Bool
|
|
|
|
|
|
|
|
public init(
|
|
public init(
|
|
|
text: Binding<Decimal>,
|
|
text: Binding<Decimal>,
|
|
@@ -27,7 +28,8 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
|
|
|
maxLength: Int? = nil,
|
|
maxLength: Int? = nil,
|
|
|
isDismissible: Bool = true,
|
|
isDismissible: Bool = true,
|
|
|
textFieldDidBeginEditing: (() -> Void)? = nil,
|
|
textFieldDidBeginEditing: (() -> Void)? = nil,
|
|
|
- numberFormatter: NumberFormatter
|
|
|
|
|
|
|
+ numberFormatter: NumberFormatter,
|
|
|
|
|
+ allowDecimalSeparator: Bool = true
|
|
|
) {
|
|
) {
|
|
|
_text = text
|
|
_text = text
|
|
|
self.placeholder = placeholder
|
|
self.placeholder = placeholder
|
|
@@ -42,6 +44,7 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
|
|
|
self.textFieldDidBeginEditing = textFieldDidBeginEditing
|
|
self.textFieldDidBeginEditing = textFieldDidBeginEditing
|
|
|
self.numberFormatter = numberFormatter
|
|
self.numberFormatter = numberFormatter
|
|
|
self.numberFormatter.numberStyle = .decimal
|
|
self.numberFormatter.numberStyle = .decimal
|
|
|
|
|
+ self.allowDecimalSeparator = allowDecimalSeparator
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public func makeUIView(context: Context) -> UITextField {
|
|
public func makeUIView(context: Context) -> UITextField {
|
|
@@ -146,7 +149,7 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
|
|
|
let isDecimalSeparator = (string == decimalFormatter.decimalSeparator && textField.text?.contains(string) == false)
|
|
let isDecimalSeparator = (string == decimalFormatter.decimalSeparator && textField.text?.contains(string) == false)
|
|
|
|
|
|
|
|
// Only proceed if the input is a valid number or decimal separator
|
|
// Only proceed if the input is a valid number or decimal separator
|
|
|
- if isNumber || isDecimalSeparator,
|
|
|
|
|
|
|
+ if isNumber || isDecimalSeparator && parent.allowDecimalSeparator,
|
|
|
let currentText = textField.text as NSString?
|
|
let currentText = textField.text as NSString?
|
|
|
{
|
|
{
|
|
|
// Get the proposed new text
|
|
// Get the proposed new text
|
|
@@ -164,7 +167,7 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Allow the change if it's a valid number or decimal separator
|
|
// Allow the change if it's a valid number or decimal separator
|
|
|
- return isNumber || isDecimalSeparator
|
|
|
|
|
|
|
+ return isNumber || isDecimalSeparator && parent.allowDecimalSeparator
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public func textFieldDidBeginEditing(_: UITextField) {
|
|
public func textFieldDidBeginEditing(_: UITextField) {
|
|
@@ -185,3 +188,124 @@ extension UIApplication {
|
|
|
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
|
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+public struct TextFieldWithToolBarString: UIViewRepresentable {
|
|
|
|
|
+ @Binding var text: String
|
|
|
|
|
+ var placeholder: String
|
|
|
|
|
+ var textAlignment: NSTextAlignment = .right
|
|
|
|
|
+ var keyboardType: UIKeyboardType = .default
|
|
|
|
|
+ var autocapitalizationType: UITextAutocapitalizationType = .none
|
|
|
|
|
+ var autocorrectionType: UITextAutocorrectionType = .no
|
|
|
|
|
+ var shouldBecomeFirstResponder: Bool = false
|
|
|
|
|
+ var maxLength: Int? = nil
|
|
|
|
|
+ var isDismissible: Bool = true
|
|
|
|
|
+
|
|
|
|
|
+ public func makeUIView(context: Context) -> UITextField {
|
|
|
|
|
+ let textField = UITextField()
|
|
|
|
|
+ context.coordinator.textField = textField
|
|
|
|
|
+ textField.inputAccessoryView = isDismissible ? makeDoneToolbar(for: textField, context: context) : nil
|
|
|
|
|
+ textField.addTarget(context.coordinator, action: #selector(Coordinator.editingDidBegin), for: .editingDidBegin)
|
|
|
|
|
+ textField.delegate = context.coordinator
|
|
|
|
|
+ textField.text = text
|
|
|
|
|
+ textField.placeholder = placeholder
|
|
|
|
|
+ textField.textAlignment = textAlignment
|
|
|
|
|
+ textField.keyboardType = keyboardType
|
|
|
|
|
+ textField.autocapitalizationType = autocapitalizationType
|
|
|
|
|
+ textField.autocorrectionType = autocorrectionType
|
|
|
|
|
+ textField.adjustsFontSizeToFitWidth = true
|
|
|
|
|
+ return textField
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private func makeDoneToolbar(for textField: UITextField, context: Context) -> UIToolbar {
|
|
|
|
|
+ let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
|
|
|
|
|
+ let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
|
|
|
|
|
+ let doneButton = UIBarButtonItem(
|
|
|
|
|
+ image: UIImage(systemName: "keyboard.chevron.compact.down"),
|
|
|
|
|
+ style: .done,
|
|
|
|
|
+ target: textField,
|
|
|
|
|
+ action: #selector(UITextField.resignFirstResponder)
|
|
|
|
|
+ )
|
|
|
|
|
+ let clearButton = UIBarButtonItem(
|
|
|
|
|
+ image: UIImage(systemName: "trash"),
|
|
|
|
|
+ style: .plain,
|
|
|
|
|
+ target: context.coordinator,
|
|
|
|
|
+ action: #selector(Coordinator.clearText)
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ toolbar.items = [clearButton, flexibleSpace, doneButton]
|
|
|
|
|
+ toolbar.sizeToFit()
|
|
|
|
|
+ return toolbar
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public func updateUIView(_ textField: UITextField, context: Context) {
|
|
|
|
|
+ if textField.text != text {
|
|
|
|
|
+ textField.text = text
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ textField.textAlignment = textAlignment
|
|
|
|
|
+ textField.keyboardType = keyboardType
|
|
|
|
|
+ textField.autocapitalizationType = autocapitalizationType
|
|
|
|
|
+ textField.autocorrectionType = autocorrectionType
|
|
|
|
|
+
|
|
|
|
|
+ if shouldBecomeFirstResponder, !context.coordinator.didBecomeFirstResponder {
|
|
|
|
|
+ if textField.window != nil, textField.becomeFirstResponder() {
|
|
|
|
|
+ context.coordinator.didBecomeFirstResponder = true
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if !shouldBecomeFirstResponder, context.coordinator.didBecomeFirstResponder {
|
|
|
|
|
+ context.coordinator.didBecomeFirstResponder = false
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public func makeCoordinator() -> Coordinator {
|
|
|
|
|
+ Coordinator(self, maxLength: maxLength)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public final class Coordinator: NSObject {
|
|
|
|
|
+ var parent: TextFieldWithToolBarString
|
|
|
|
|
+ var textField: UITextField?
|
|
|
|
|
+ let maxLength: Int?
|
|
|
|
|
+ var didBecomeFirstResponder = false
|
|
|
|
|
+
|
|
|
|
|
+ init(_ parent: TextFieldWithToolBarString, maxLength: Int?) {
|
|
|
|
|
+ self.parent = parent
|
|
|
|
|
+ self.maxLength = maxLength
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @objc fileprivate func clearText() {
|
|
|
|
|
+ parent.text = ""
|
|
|
|
|
+ textField?.text = ""
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @objc fileprivate func editingDidBegin(_ textField: UITextField) {
|
|
|
|
|
+ DispatchQueue.main.async {
|
|
|
|
|
+ textField.moveCursorToEnd()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+extension TextFieldWithToolBarString.Coordinator: UITextFieldDelegate {
|
|
|
|
|
+ public func textField(
|
|
|
|
|
+ _ textField: UITextField,
|
|
|
|
|
+ shouldChangeCharactersIn range: NSRange,
|
|
|
|
|
+ replacementString string: String
|
|
|
|
|
+ ) -> Bool {
|
|
|
|
|
+ if let maxLength = parent.maxLength {
|
|
|
|
|
+ // Get the current text, including the proposed change
|
|
|
|
|
+ let currentText = textField.text ?? ""
|
|
|
|
|
+ let newLength = currentText.count + string.count - range.length
|
|
|
|
|
+ if newLength > maxLength {
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DispatchQueue.main.async {
|
|
|
|
|
+ if let textFieldText = textField.text as NSString? {
|
|
|
|
|
+ let newText = textFieldText.replacingCharacters(in: range, with: string)
|
|
|
|
|
+ self.parent.text = newText
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return true
|
|
|
|
|
+ }
|
|
|
|
|
+}
|