From df34e1574c31ecc5770fab9fc30144b825bbaad4 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Mon, 18 Nov 2019 17:04:16 -0500 Subject: [PATCH] latest, updating. --- MVMCoreUI/Atoms/TextFields/DigitBox.swift | 83 ++++++++++++------- .../Atoms/TextFields/DigitEntryField.swift | 42 +++------- 2 files changed, 62 insertions(+), 63 deletions(-) diff --git a/MVMCoreUI/Atoms/TextFields/DigitBox.swift b/MVMCoreUI/Atoms/TextFields/DigitBox.swift index 1c1776d8..c0b161b3 100644 --- a/MVMCoreUI/Atoms/TextFields/DigitBox.swift +++ b/MVMCoreUI/Atoms/TextFields/DigitBox.swift @@ -18,8 +18,11 @@ import UIKit // MARK: - Outlets //-------------------------------------------------- - let digitField: UITextField = { - let textField = UITextField(frame: .zero) + let digitField: TextField = { + let textField = TextField() + textField.isAccessibilityElement = true + textField.setContentCompressionResistancePriority(.required, for: .vertical) + textField.setContentCompressionResistancePriority(.required, for: .horizontal) textField.textAlignment = .center textField.keyboardType = .numberPad return textField @@ -31,6 +34,27 @@ import UIKit private var previousSize: CGFloat = 0.0 + //-------------------------------------------------- + // MARK: - Property Observer + //-------------------------------------------------- + + public override var showError: Bool { + didSet { + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } + + self.borderStrokeColor = self.showError ? .mfPumpkin() : .mfSilver() + + let barHeight: CGFloat = self.showError ? 4 : 1 + self.bottomBar.frame = CGRect(x: 0, y: self.bounds.height - barHeight, width: self.bounds.width, height: barHeight) + self.bottomBar.backgroundColor = self.showError ? UIColor.mfPumpkin().cgColor : UIColor.black.cgColor + + self.setNeedsDisplay() + self.layoutIfNeeded() + } + } + } + //-------------------------------------------------- // MARK: - Delegate //-------------------------------------------------- @@ -59,7 +83,7 @@ import UIKit required public init?(coder: NSCoder) { super.init(coder: coder) - fatalError("DigitBox has not been implemented") + fatalError("DigitBox does not support xibs.") } //-------------------------------------------------- @@ -72,12 +96,19 @@ import UIKit translatesAutoresizingMaskIntoConstraints = false backgroundColor = .white + + addSubview(digitField) digitField.delegate = self - showErrorState(false) + + NSLayoutConstraint.activate([ + digitField.heightAnchor.constraint(equalToConstant: 24), + digitField.topAnchor.constraint(equalTo: topAnchor, constant: 12), + digitField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12), + bottomAnchor.constraint(equalTo: digitField.bottomAnchor, constant: 12), + trailingAnchor.constraint(equalTo: digitField.trailingAnchor, constant: 12)]) widthConstraint = widthAnchor.constraint(equalToConstant: 39) widthConstraint?.isActive = true - heightConstraint = heightAnchor.constraint(equalToConstant: 44) heightConstraint?.isActive = true @@ -96,14 +127,14 @@ import UIKit // MARK: - Methods //-------------------------------------------------- - public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { - - if string.isBackspace { - textBoxDelegate?.textFieldDidDelete?(self.digitField) - } - - return true - } +// public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { +// +// if string.isBackspace { +// textBoxDelegate?.textFieldDidDelete?(self.digitField) +// } +// +// return true +// } public override func updateView(_ size: CGFloat) { super.updateView(size) @@ -140,26 +171,14 @@ import UIKit self.setNeedsLayout() } } - - func showErrorState(_ show: Bool) { - - showError = show - borderStrokeColor = show ? .mfPumpkin() : .mfSilver() - - let barHeight: CGFloat = show ? 4 : 1 - bottomBar.frame = CGRect(x: 0, y: bounds.height - barHeight, width: bounds.width, height: barHeight) - bottomBar.backgroundColor = show ? UIColor.mfPumpkin().cgColor : UIColor.black.cgColor - - setNeedsDisplay() - layoutIfNeeded() - } } // TODO: Move if working properly. -extension String { - var isBackspace: Bool { - let char = self.cString(using: String.Encoding.utf8)! - return strcmp(char, "\\b") == -92 - } -} +//extension String { +// +// var isBackspace: Bool { +// let char = self.cString(using: String.Encoding.utf8)! +// return strcmp(char, "\\b") == -92 +// } +//} diff --git a/MVMCoreUI/Atoms/TextFields/DigitEntryField.swift b/MVMCoreUI/Atoms/TextFields/DigitEntryField.swift index b352a89b..5fae6aa9 100644 --- a/MVMCoreUI/Atoms/TextFields/DigitEntryField.swift +++ b/MVMCoreUI/Atoms/TextFields/DigitEntryField.swift @@ -21,6 +21,10 @@ import UIKit public var digitFields: [DigitBox] = [] + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + //-------------------------------------------------- // MARK: - Property Observers //-------------------------------------------------- @@ -31,7 +35,6 @@ import UIKit digitFields.forEach { $0.isEnabled = self.isEnabled - $0.isUserInteractionEnabled = isEnabled $0.digitField.isEnabled = isEnabled $0.digitField.textColor = isEnabled ? .black : .mfBattleshipGrey() @@ -42,7 +45,6 @@ import UIKit public override var placeholder: String? { get { var string = "" - digitFields.forEach { string += $0.digitField.attributedPlaceholder?.string ?? "" } return !string.isEmpty ? string : nil @@ -58,7 +60,7 @@ import UIKit } } - // If there is already text in the textfield, set the place holder label below. + // If there is already text in the textfield, set the placeholder label below. if let text = text, !text.isEmpty && !showError { feedback = placeholder } else if !showError { @@ -78,7 +80,6 @@ import UIKit public override var text: String? { get { var string = "" - digitFields.forEach { string += $0.digitField.text ?? "" } return string @@ -109,9 +110,8 @@ import UIKit self.init(frame: .zero) } - public init(numberOfDigits: Int) { - super.init(frame: .zero) - + public convenience init(numberOfDigits: Int) { + self.init(frame: .zero) self.numberOfDigits = numberOfDigits } @@ -133,9 +133,10 @@ import UIKit alignCenterHorizontal() isAccessibilityElement = false + entryContainer.hideBorder = true entryContainer.bottomBar.backgroundColor = UIColor.clear.cgColor entryContainer.bottomBar.frame = CGRect(x: 0, y: entryContainer.bounds.height, width: 0, height: 0) - setupDigitFieldsView(size: MVMCoreUISplitViewController.getDetailViewWidth()) + assembleDigitFieldsView(size: MVMCoreUISplitViewController.getDetailViewWidth()) textField.removeFromSuperview() } @@ -149,7 +150,7 @@ import UIKit return digitBox } - func setupDigitFieldsView(size: CGFloat) { + func assembleDigitFieldsView(size: CGFloat) { var accessibleElements: [Any] = [titleLabel] @@ -166,7 +167,6 @@ import UIKit box.topAnchor.constraint(equalTo: entryContainer.topAnchor).isActive = true entryContainer.bottomAnchor.constraint(equalTo: box.bottomAnchor).isActive = true - box.centerYAnchor.constraint(equalTo: entryContainer.centerYAnchor).isActive = true if index == 0 { box.leadingAnchor.constraint(equalTo: entryContainer.leadingAnchor).isActive = true @@ -203,33 +203,13 @@ import UIKit self.digitFields.forEach { $0.updateView(size) } self.layoutIfNeeded() } - - // Layout text boxes. -// self.setupDigitFieldsView(size: size) } -// hideBorder = true } //-------------------------------------------------- // MARK: - Methods //-------------------------------------------------- - override func valueChanged() { - super.valueChanged() - - // TODO: is feedbackContainerDistance needed? -// DispatchQueue.main.async { [weak self] in -// guard let self = self else { return } -// -// if let feedback = self.feedback, !feedback.isEmpty { -// self.feedbackContainerDistance?.constant = 10 -// -// } else { -// self.feedbackContainerDistance?.constant = 0 -// } -// } - } - func setAsSecureTextEntry(_ secureEntry: Bool) { DispatchQueue.main.async { [weak self] in @@ -411,7 +391,7 @@ extension DigitEntryField { } } - setupDigitFieldsView(size: MVMCoreUIUtility.getWidth()) + assembleDigitFieldsView(size: MVMCoreUIUtility.getWidth()) } open override class func estimatedHeight(forRow json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat {