diff --git a/MVMCoreUI/Atomic/Atoms/TextFields/DigitBox.swift b/MVMCoreUI/Atomic/Atoms/TextFields/DigitBox.swift index 1134a80f..a155c5bd 100644 --- a/MVMCoreUI/Atomic/Atoms/TextFields/DigitBox.swift +++ b/MVMCoreUI/Atomic/Atoms/TextFields/DigitBox.swift @@ -13,7 +13,7 @@ import UIKit } -@objcMembers open class DigitBox: EntryFieldContainer, UITextFieldDelegate, TextFieldDidDeleteProtocol { +@objcMembers open class DigitBox: EntryFieldContainer, UITextFieldDelegate, TextInputDidDeleteProtocol { //-------------------------------------------------- // MARK: - Outlets //-------------------------------------------------- diff --git a/MVMCoreUI/BaseClasses/TextField.swift b/MVMCoreUI/BaseClasses/TextField.swift index c571d52d..ea56e68e 100644 --- a/MVMCoreUI/BaseClasses/TextField.swift +++ b/MVMCoreUI/BaseClasses/TextField.swift @@ -8,7 +8,7 @@ import UIKit -public protocol TextFieldDidDeleteProtocol: class { +public protocol TextInputDidDeleteProtocol: class { func textFieldDidDelete() } @@ -17,6 +17,7 @@ public protocol TextFieldDidDeleteProtocol: class { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- + private var initialSetupPerformed = false /// Set to true to hide the blinking textField cursor. @@ -27,7 +28,7 @@ public protocol TextFieldDidDeleteProtocol: class { //-------------------------------------------------- /// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well. - public weak var didDeleteDelegate: TextFieldDidDeleteProtocol? + public weak var didDeleteDelegate: TextInputDidDeleteProtocol? //-------------------------------------------------- // MARK: - Initialization diff --git a/MVMCoreUI/BaseClasses/TextView.swift b/MVMCoreUI/BaseClasses/TextView.swift index b0076c09..977dcfe8 100644 --- a/MVMCoreUI/BaseClasses/TextView.swift +++ b/MVMCoreUI/BaseClasses/TextView.swift @@ -18,30 +18,27 @@ import UIKit private var initialSetupPerformed = false - /// Set this property if you want updateView to update the font based on this standard and the size passed in. - public var standardFontSize: CGFloat = 0.0 - + /// If true then text textView is currently displaying the stored placeholder text as there is not content to display. public var isShowingPlaceholder = true /// Set to true to hide the blinking textField cursor. public var hideBlinkingCaret = false - private var textTraits: (color: UIColor, font: UIFont) = (color: .mvmBlack, - font: Styler.Font.RegularBodySmall.getFont()) - private var placeholderTraits: (text: String, color: UIColor, font: UIFont) = (text: "", - color: .mvmCoolGray3, font: Styler.Font.RegularMicro.getFont()) + public var textViewModel: TextViewModel? { + return model as? TextViewModel + } //-------------------------------------------------- // MARK: - Delegate //-------------------------------------------------- /// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well. - public weak var didDeleteDelegate: TextFieldDidDeleteProtocol? + public weak var didDeleteDelegate: TextInputDidDeleteProtocol? /// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well. private weak var proprietorTextDelegate: UITextViewDelegate? - /// If you're using a ViewController, you must set this to it + /// If you're using a ViewController, you must set this to it. public weak var uiTextViewDelegate: UITextViewDelegate? { get { return delegate } set { @@ -74,12 +71,15 @@ import UIKit initialSetup() } - convenience init(placeholderText: String, delegate: UITextViewDelegate?) { + convenience init(delegate: UITextViewDelegate) { self.init(frame: .zero, textContainer: nil) self.delegate = delegate - self.placeholderTraits.text = placeholderText } + //-------------------------------------------------- + // MARK: - Setup + //-------------------------------------------------- + public func initialSetup() { if !initialSetupPerformed { @@ -89,6 +89,11 @@ import UIKit } } + //-------------------------------------------------- + // MARK: - Methods + //-------------------------------------------------- + + /// Alters the blinking caret line as per design standards. open override func caretRect(for position: UITextPosition) -> CGRect { if hideBlinkingCaret { @@ -104,26 +109,40 @@ import UIKit didDeleteDelegate?.textFieldDidDelete() } - open func setTextViewState() { + public func setTextAppearance() { if isShowingPlaceholder { - text = "" - font = textTraits.font - textColor = textTraits.color - isShowingPlaceholder = false - } else if text.isEmpty { - isShowingPlaceholder = true - textColor = placeholderTraits.color - text = placeholderTraits.text - font = placeholderTraits.font + setTextContentTraits() } } + public func setPlaceholderIfAvailable() { + + if let placeholder = textViewModel?.placeholder, !placeholder.isEmpty && text.isEmpty { + setPlaceholderContentTraits() + } + } + + public func setTextContentTraits() { + + isShowingPlaceholder = false + text = "" + font = textViewModel?.fontStyle.getFont() + textColor = textViewModel?.textColor.uiColor + } + + public func setPlaceholderContentTraits() { + + isShowingPlaceholder = true + textColor = textViewModel?.placeholderTextColor.uiColor + font = textViewModel?.placeholderFontStyle.getFont() + text = textViewModel?.placeholder + } + //-------------------------------------------------- // MARK: - UITextViewDelegate //-------------------------------------------------- - //#pragma Mark UITextView Delegate methods forwarding @objc public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { return proprietorTextDelegate?.textViewShouldBeginEditing?(textView) ?? true @@ -131,6 +150,7 @@ import UIKit @objc public func textViewDidBeginEditing(_ textView: UITextView) { + setTextAppearance() proprietorTextDelegate?.textViewDidBeginEditing?(textView) } @@ -141,8 +161,6 @@ import UIKit @objc public func textViewDidChange(_ textView: UITextView) { - setTextViewState() - proprietorTextDelegate?.textViewDidChange?(textView) } @@ -153,6 +171,7 @@ import UIKit @objc public func textViewDidEndEditing(_ textView: UITextView) { + setPlaceholderIfAvailable() proprietorTextDelegate?.textViewDidEndEditing?(textView) } } @@ -174,7 +193,7 @@ extension TextView: MVMCoreViewProtocol { smartQuotesType = .no smartDashesType = .no smartInsertDeleteType = .no - font = textTraits.font + font = textViewModel?.fontStyle.getFont() layer.borderWidth = 1 layer.borderColor = UIColor.mvmBlack.cgColor isEditable = true @@ -193,10 +212,8 @@ extension TextView: MoleculeViewProtocol { guard let model = model as? TextViewModel else { return } - if let height = model.height { - heightConstraint = heightAnchor.constraint(equalToConstant: height) - heightConstraint?.isActive = true - } + heightConstraint = heightAnchor.constraint(equalToConstant: model.height) + heightConstraint?.isActive = true isEditable = model.isEditable textAlignment = model.textAlignment @@ -204,32 +221,15 @@ extension TextView: MoleculeViewProtocol { layer.borderColor = model.borderColor?.cgColor layer.borderWidth = model.borderWidth text = model.text - isShowingPlaceholder = model.text.isEmpty - placeholderTraits.font = model.placeholderFont.getFont() - placeholderTraits.text = model.placeholder uiTextViewDelegate = delegateObject?.uiTextViewDelegate + isShowingPlaceholder = model.text.isEmpty if let accessibilityText = model.accessibilityText { accessibilityLabel = accessibilityText } font = model.fontStyle.getFont() - standardFontSize = model.fontStyle.pointSize() - if let font = font { - textTraits.font = font - } - - let fontSize = model.fontSize - if let fontSize = fontSize { - standardFontSize = fontSize - } - if let fontName = model.fontName { - font = Styler.Font(rawValue: fontName)?.getFont() - textTraits.font = font! - } else if let fontSize = fontSize { - font = font?.updateSize(fontSize) - textTraits.font = font! - } + setPlaceholderIfAvailable() if isEditable { MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegateObject?.uiTextViewDelegate) @@ -238,8 +238,10 @@ extension TextView: MoleculeViewProtocol { open func reset() { - setNeedsDisplay() backgroundColor = .clear text = "" + inputAccessoryView?.removeFromSuperview() + layer.borderColor = UIColor.mvmBlack.cgColor + layer.borderWidth = 0 } } diff --git a/MVMCoreUI/BaseClasses/TextViewModel.swift b/MVMCoreUI/BaseClasses/TextViewModel.swift index 7cff3119..296e2ea8 100644 --- a/MVMCoreUI/BaseClasses/TextViewModel.swift +++ b/MVMCoreUI/BaseClasses/TextViewModel.swift @@ -16,20 +16,19 @@ open class TextViewModel: MoleculeModelProtocol { public static var identifier: String = "textView" public var backgroundColor: Color? - public var text: String = "" public var accessibilityText: String? + public var text: String = "" public var textColor: Color = Color(uiColor: .mvmBlack) - public var fontSize: CGFloat? - public var fontName: String? public var fontStyle: Styler.Font = Styler.Font.RegularBodySmall public var textAlignment: NSTextAlignment = .left - public var height: CGFloat? + public var height: CGFloat public var placeholder: String = "" - public var placeholderFont: Styler.Font = Styler.Font.RegularMicro + public var placeholderTextColor: Color = Color(uiColor: .mvmCoolGray3) + public var placeholderFontStyle: Styler.Font = Styler.Font.RegularMicro + public var showsPlaceholder: Bool = true public var isEditable: Bool = true public var borderColor: Color? public var borderWidth: CGFloat = 0 - public var showsPlaceholder: Bool = true //-------------------------------------------------- // MARK: - Keys @@ -42,15 +41,13 @@ open class TextViewModel: MoleculeModelProtocol { case accessibilityText case textColor case fontStyle - case fontSize - case fontName case textAlignment case attributes case height case borderColor case borderWidth case placeholder - case placeholderFont + case placeholderFontStyle case isEditable } @@ -58,7 +55,8 @@ open class TextViewModel: MoleculeModelProtocol { // MARK: - Initializer //-------------------------------------------------- - public init(text: String) { + public init(height: CGFloat, text: String = "") { + self.height = height self.text = text } @@ -77,8 +75,8 @@ open class TextViewModel: MoleculeModelProtocol { self.placeholder = placeholder } - if let placeholderFont = try typeContainer.decodeIfPresent(Styler.Font.self, forKey: .placeholderFont) { - self.placeholderFont = placeholderFont + if let placeholderFontStyle = try typeContainer.decodeIfPresent(Styler.Font.self, forKey: .placeholderFontStyle) { + self.placeholderFontStyle = placeholderFontStyle } if let textAlignment = try typeContainer.decodeIfPresent(NSTextAlignment.self, forKey: .textAlignment) { @@ -104,9 +102,7 @@ open class TextViewModel: MoleculeModelProtocol { borderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .borderColor) accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) - fontSize = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .fontSize) - fontName = try typeContainer.decodeIfPresent(String.self, forKey: .fontName) - height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height) + height = try typeContainer.decode(CGFloat.self, forKey: .height) } public func encode(to encoder: Encoder) throws { @@ -116,13 +112,11 @@ open class TextViewModel: MoleculeModelProtocol { try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText) try container.encodeIfPresent(textColor, forKey: .textColor) try container.encodeIfPresent(fontStyle, forKey: .fontStyle) - try container.encodeIfPresent(height, forKey: .height) try container.encodeIfPresent(borderColor, forKey: .borderColor) - try container.encodeIfPresent(fontSize, forKey: .fontSize) - try container.encodeIfPresent(fontName, forKey: .fontName) + try container.encode(height, forKey: .height) try container.encode(text, forKey: .text) try container.encode(placeholder, forKey: .placeholder) - try container.encode(placeholderFont, forKey: .placeholderFont) + try container.encode(placeholderFontStyle, forKey: .placeholderFontStyle) try container.encode(textAlignment, forKey: .textAlignment) try container.encode(isEditable, forKey: .isEditable) try container.encode(borderWidth, forKey: .borderWidth)