From 91cb21eb0482d4c2bcae231c2c8d8215db6fc96e Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Mon, 6 May 2024 09:24:30 -0500 Subject: [PATCH] added done button Signed-off-by: Matt Bruce --- .../TextFields/InputField/TextField.swift | 38 +++++++++++++++++++ .../TextFields/TextArea/TextView.swift | 19 +++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/VDS/Components/TextFields/InputField/TextField.swift b/VDS/Components/TextFields/InputField/TextField.swift index 14178020..9a96829e 100644 --- a/VDS/Components/TextFields/InputField/TextField.swift +++ b/VDS/Components/TextFields/InputField/TextField.swift @@ -10,6 +10,25 @@ import UIKit @objc(VDSTextField) open class TextField: UITextField { + + //-------------------------------------------------- + // MARK: - Initializers + //-------------------------------------------------- + required public init() { + super.init(frame: .zero) + initialSetup() + } + + public override init(frame: CGRect) { + super.init(frame: .zero) + initialSetup() + } + + public required init?(coder: NSCoder) { + super.init(coder: coder) + initialSetup() + } + var horizontalPadding: CGFloat = 0 open override func textRect(forBounds bounds: CGRect) -> CGRect { @@ -35,6 +54,25 @@ open class TextField: UITextField { } } + open func initialSetup() { + let doneToolbar: UIToolbar = UIToolbar() + doneToolbar.translatesAutoresizingMaskIntoConstraints = false + doneToolbar.barStyle = .default + + let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) + let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction)) + done.accessibilityHint = "Double tap to finish editing." + doneToolbar.items = [flexSpace, done] + doneToolbar.sizeToFit() + + inputAccessoryView = doneToolbar + } + + @objc func doneButtonAction() { + // Resigns the first responder status when 'Done' is tapped + resignFirstResponder() + } + open override func becomeFirstResponder() -> Bool { let success = super.becomeFirstResponder() if isSecureTextEntry, let text { diff --git a/VDS/Components/TextFields/TextArea/TextView.swift b/VDS/Components/TextFields/TextArea/TextView.swift index c35b4a80..efe8164b 100644 --- a/VDS/Components/TextFields/TextArea/TextView.swift +++ b/VDS/Components/TextFields/TextArea/TextView.swift @@ -97,7 +97,6 @@ open class TextView: UITextView, ViewProtocol { initialSetupPerformed = true backgroundColor = .clear translatesAutoresizingMaskIntoConstraints = false - accessibilityCustomActions = [] setup() setNeedsUpdate() } @@ -106,8 +105,25 @@ open class TextView: UITextView, ViewProtocol { open func setup() { translatesAutoresizingMaskIntoConstraints = false + let doneToolbar: UIToolbar = UIToolbar() + doneToolbar.translatesAutoresizingMaskIntoConstraints = false + doneToolbar.barStyle = .default + + let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) + let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction)) + done.accessibilityHint = "Double tap to finish editing." + doneToolbar.items = [flexSpace, done] + doneToolbar.sizeToFit() + + inputAccessoryView = doneToolbar + } + @objc func doneButtonAction() { + // Resigns the first responder status when 'Done' is tapped + resignFirstResponder() + } + open func updateView() { updateLabel() } @@ -118,7 +134,6 @@ open class TextView: UITextView, ViewProtocol { shouldUpdateView = false surface = .light text = nil - accessibilityCustomActions = [] shouldUpdateView = true setNeedsUpdate() }