Keeping up appearance.

This commit is contained in:
Kevin G Christiano 2019-10-30 16:02:18 -04:00
parent 9d043edb29
commit 5c1d7b4a38
2 changed files with 57 additions and 98 deletions

View File

@ -19,7 +19,7 @@ import UIKit
// MARK: - Outlets
//--------------------------------------------------
private(set) var titleLabel: Label = {
public private(set) var titleLabel: Label = {
let label = Label()
label.font = MFStyler.fontB3()
label.textColor = UIColor.mfBattleshipGrey()
@ -28,7 +28,7 @@ import UIKit
return label
}()
private(set) var feedbackLabel: Label = {
public private(set) var feedbackLabel: Label = {
let label = Label()
label.font = MFStyler.fontForTextFieldUnderLabel()
label.textColor = .black
@ -37,7 +37,7 @@ import UIKit
return label
}()
private(set) var fieldContainer: UIView = {
public private(set) var fieldContainer: UIView = {
let view = UIView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
return view
@ -65,13 +65,11 @@ import UIKit
public var fieldKey: String?
/// Determines if a border should be drawn.
public var hideBorder = false
private var hideBorder = false
public var showErrorMessage = false {
didSet {
feedbackHeightConstraint?.isActive = !showErrorMessage
}
}
public private(set) var appearance: Appearance = .original
public var showErrorMessage = false
public var errorMessage: String?
@ -81,7 +79,7 @@ import UIKit
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.isEnabled ? self.originalAppearance() : self.disabledAppearance()
self.updateUI(appearance: self.isEnabled ? .original : .disable)
}
}
}
@ -106,7 +104,7 @@ import UIKit
set {
feedbackLabel.text = newValue
setAccessibilityString(newValue)
refreshUI()
refreshBorderUI()
}
}
@ -131,13 +129,10 @@ import UIKit
public var feedbackLabelTrailing: NSLayoutConstraint?
public var feedbackLabelLeading: NSLayoutConstraint?
var feedbackHeightConstraint: NSLayoutConstraint?
public var titleLabelLeading: NSLayoutConstraint?
public var titleLabelTrailing: NSLayoutConstraint?
public var bottomBarHeightConstraint: NSLayoutConstraint?
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
@ -195,7 +190,6 @@ import UIKit
addSubview(feedbackLabel)
feedbackHeightConstraint = feedbackLabel.heightAnchor.constraint(equalToConstant: 0)
feedbackLabel.topAnchor.constraint(equalTo: fieldContainer.bottomAnchor, constant: PaddingOne).isActive = true
feedbackLabelLeading = feedbackLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor)
feedbackLabelLeading?.isActive = true
@ -222,7 +216,7 @@ import UIKit
titleLabel.updateView(size)
feedbackLabel.font = MFStyler.fontForTextFieldUnderLabel()
refreshUI()
refreshBorderUI()
}
open override func reset() {
@ -232,20 +226,19 @@ import UIKit
titleLabel.reset()
feedbackLabel.reset()
fieldContainer.subviews.forEach { $0.removeFromSuperview() }
originalAppearance()
updateUI(appearance: .original)
}
open override func layoutSubviews() {
super.layoutSubviews()
refreshUI(bottomBarSize: 1)
refreshBorderUI(bottomBarSize: 1)
}
open func refreshUI(bottomBarSize: CGFloat? = nil) {
open func refreshBorderUI(bottomBarSize: CGFloat? = nil) {
if let size = bottomBarSize {
bottomBar.frame = CGRect(x: 0, y: fieldContainer.bounds.height - size, width: fieldContainer.bounds.width, height: size)
}
let size = CGFloat(appearance == .error ? 4 : 1)
bottomBar.frame = CGRect(x: 0, y: fieldContainer.bounds.height - size, width: fieldContainer.bounds.width, height: size)
self.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated?(self)
setNeedsDisplay()
@ -313,66 +306,50 @@ import UIKit
case error
case lock
case select
case disabled
case disable
}
open func originalAppearance() {
isUserInteractionEnabled = true
bottomBarHeightConstraint?.constant = 1
hideBorder = false
showErrorMessage = false
borderStrokeColor = .mfSilver()
bottomBar.backgroundColor = UIColor.black.cgColor
titleLabel.textColor = .mfBattleshipGrey()
refreshUI(bottomBarSize: 1)
}
/// - parameter showError: Default = false. Determines if error message should be displayed when setting the visual error appearance.
open func errorAppearance(showError: Bool = false) {
public func updateUI(appearance: Appearance) {
self.appearance = appearance
isUserInteractionEnabled = true
hideBorder = false
bottomBarHeightConstraint?.constant = 4
borderStrokeColor = .mfPumpkin()
bottomBar.backgroundColor = UIColor.mfPumpkin().cgColor
if showError {
showErrorMessage = true
feedback = errorMessage
switch appearance {
case .original:
borderStrokeColor = .mfSilver()
feedback = nil
bottomBar.backgroundColor = UIColor.black.cgColor
titleLabel.textColor = .mfBattleshipGrey()
case .error:
borderStrokeColor = .mfPumpkin()
titleLabel.textColor = UIColor.mfBattleshipGrey()
bottomBar.backgroundColor = UIColor.mfPumpkin().cgColor
feedback = showErrorMessage ? errorMessage : nil
case .lock:
isUserInteractionEnabled = false
hideBorder = true
feedback = nil
titleLabel.textColor = UIColor.mfBattleshipGrey()
bottomBar.backgroundColor = UIColor.clear.cgColor
case .select:
borderStrokeColor = .black
feedback = nil
titleLabel.textColor = UIColor.mfBattleshipGrey()
bottomBar.backgroundColor = UIColor.black.cgColor
case .disable:
isUserInteractionEnabled = false
feedback = nil
borderStrokeColor = .mfSilver()
titleLabel.textColor = self.isEnabled ? UIColor.mfBattleshipGrey() : UIColor.mfSilver()
bottomBar.backgroundColor = self.isEnabled ? UIColor.black.cgColor : UIColor.mfSilver().cgColor
}
refreshUI(bottomBarSize: 4)
}
open func lockAppearance() {
isUserInteractionEnabled = false
bottomBarHeightConstraint?.constant = 1
hideBorder = true
bottomBar.backgroundColor = UIColor.clear.cgColor
refreshUI(bottomBarSize: 1)
}
open func selectedAppearance() {
isUserInteractionEnabled = true
bottomBarHeightConstraint?.constant = 1
hideBorder = false
borderStrokeColor = .black
bottomBar.backgroundColor = UIColor.black.cgColor
refreshUI(bottomBarSize: 1)
}
open func disabledAppearance() {
isUserInteractionEnabled = false
bottomBarHeightConstraint?.constant = 1
hideBorder = false
feedback = nil
titleLabel.textColor = self.isEnabled ? UIColor.mfBattleshipGrey() : UIColor.mfSilver()
bottomBar.backgroundColor = self.isEnabled ? UIColor.black.cgColor : UIColor.mfSilver().cgColor
refreshUI(bottomBarSize: 1)
refreshBorderUI(bottomBarSize: appearance == .error ? 4 : 1)
}
}
@ -403,24 +380,8 @@ extension FormEntryField {
self.hideBorder = hideBorder
}
if let appearance = dictionary["appearance"] as? String {
switch Appearance(rawValue: appearance) {
case .error:
errorAppearance()
case .lock:
lockAppearance()
case .select:
selectedAppearance()
case .disabled:
// This is set by the isEnabled property observer.
isEnabled = false
default:
originalAppearance()
}
if let appearance = dictionary["appearance"] as? String, let kind = Appearance(rawValue: appearance) {
updateUI(appearance: kind)
}
// Key used to send text value to server

View File

@ -50,7 +50,7 @@ import UIKit
guard let self = self else { return }
self.textField.isEnabled = self.isEnabled
self.textField.textColor = self.isEnabled ? self.enabledTextColor : self.enabledTextColor
self.textField.textColor = self.isEnabled ? self.enabledTextColor : self.disabledTextColor
}
}
}
@ -115,7 +115,7 @@ import UIKit
public override init(frame: CGRect) {
super.init(frame: frame)
setupView()
// setupView()
}
public convenience init() {
@ -126,7 +126,7 @@ import UIKit
public init(bothDelegates: (UITextFieldDelegate & TextFieldDelegate)?) {
super.init(frame: .zero)
setupView()
// setupView()
setBothTextDelegates(bothDelegates)
}
@ -158,10 +158,8 @@ import UIKit
open override func updateView(_ size: CGFloat) {
super.updateView(size)
MFStyler.styleTextField(textField)
layoutIfNeeded()
}