added accessibility

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-05-24 10:42:31 -05:00
parent 3cc00e5260
commit 8cc87fd471
2 changed files with 94 additions and 27 deletions

View File

@ -7,9 +7,10 @@
import Foundation
import UIKit
import Combine
@objc(VDSTextField)
open class TextField: UITextField {
open class TextField: UITextField, ViewProtocol, Errorable {
//--------------------------------------------------
// MARK: - Initializers
@ -29,8 +30,84 @@ open class TextField: UITextField {
initialSetup()
}
var horizontalPadding: CGFloat = 0
//--------------------------------------------------
// MARK: - Combine Properties
//--------------------------------------------------
/// Set of Subscribers for any Publishers for this Control.
open var subscribers = Set<AnyCancellable>()
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var initialSetupPerformed = false
private var horizontalPadding: CGFloat = 0
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
/// Key of whether or not updateView() is called in setNeedsUpdate()
open var shouldUpdateView: Bool = true
open var surface: Surface = .light { didSet { setNeedsUpdate() } }
open var showError: Bool = false { didSet { setNeedsUpdate() } }
open var errorText: String? { didSet { setNeedsUpdate() } }
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
open func initialSetup() {
if !initialSetupPerformed {
initialSetupPerformed = true
backgroundColor = .clear
translatesAutoresizingMaskIntoConstraints = false
setup()
setNeedsUpdate()
}
}
open func setup() {
let accessView = UIView(frame: .init(origin: .zero, size: .init(width: UIScreen.main.bounds.width, height: 44)))
accessView.backgroundColor = .white
accessView.addBorder(side: .top, width: 1, color: .lightGray)
let done = UIButton(type: .system)
done.setTitle("Done", for: .normal)
done.translatesAutoresizingMaskIntoConstraints = false
done.addTarget(self, action: #selector(doneButtonAction), for: .touchUpInside)
accessView.addSubview(done)
done.pinCenterY()
.pinTrailing(16)
inputAccessoryView = accessView
}
@objc func doneButtonAction() {
// Resigns the first responder status when 'Done' is tapped
resignFirstResponder()
}
open func updateView() {}
open func updateAccessibility() {
if let errorText, showError {
accessibilityLabel = "error, \(errorText)"
} else {
accessibilityLabel = nil
}
}
open func reset() {
shouldUpdateView = false
surface = .light
text = nil
shouldUpdateView = true
setNeedsUpdate()
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
open override func textRect(forBounds bounds: CGRect) -> CGRect {
let rect = super.textRect(forBounds: bounds)
return rect.insetBy(dx: -horizontalPadding, dy: 0)
@ -54,25 +131,6 @@ open class TextField: UITextField {
}
}
open func initialSetup() {
let accessView = UIView(frame: .init(origin: .zero, size: .init(width: UIScreen.main.bounds.width, height: 44)))
accessView.backgroundColor = .white
accessView.addBorder(side: .top, width: 1, color: .lightGray)
let done = UIButton(type: .system)
done.setTitle("Done", for: .normal)
done.translatesAutoresizingMaskIntoConstraints = false
done.addTarget(self, action: #selector(doneButtonAction), for: .touchUpInside)
accessView.addSubview(done)
done.pinCenterY()
.pinTrailing(16)
inputAccessoryView = accessView
}
@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 {

View File

@ -11,7 +11,7 @@ import Combine
import VDSTokens
@objc(VDSTextView)
open class TextView: UITextView, ViewProtocol {
open class TextView: UITextView, ViewProtocol, Errorable {
//--------------------------------------------------
// MARK: - Initializers
@ -66,6 +66,10 @@ open class TextView: UITextView, ViewProtocol {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false)
}.eraseToAnyColorable(){ didSet { setNeedsUpdate() }}
open var showError: Bool = false { didSet { setNeedsUpdate() } }
open var errorText: String? { didSet { setNeedsUpdate() } }
open override var textColor: UIColor? {
get { textColorConfiguration.getColor(self) }
set { }
@ -102,7 +106,6 @@ open class TextView: UITextView, ViewProtocol {
}
}
open func setup() {
let accessView = UIView(frame: .init(origin: .zero, size: .init(width: UIScreen.main.bounds.width, height: 44)))
accessView.backgroundColor = .white
@ -126,7 +129,13 @@ open class TextView: UITextView, ViewProtocol {
updateLabel()
}
open func updateAccessibility() {}
open func updateAccessibility() {
if let errorText, showError {
accessibilityLabel = "error, \(errorText)"
} else {
accessibilityLabel = nil
}
}
open func reset() {
shouldUpdateView = false