diff --git a/MVMCoreUI/Atomic/Atoms/TextFields/TextEntryField.swift b/MVMCoreUI/Atomic/Atoms/TextFields/TextEntryField.swift index 2e7df5ee..564f669b 100644 --- a/MVMCoreUI/Atomic/Atoms/TextFields/TextEntryField.swift +++ b/MVMCoreUI/Atomic/Atoms/TextFields/TextEntryField.swift @@ -129,7 +129,7 @@ import UIKit } } - /// If you're using a MFViewController, you must set this to it + /// If you're using a ViewController, you must set this to it public weak var uiTextFieldDelegate: UITextFieldDelegate? { get { return textField.delegate } set { textField.delegate = newValue } diff --git a/MVMCoreUI/Atomic/Atoms/TextFields/TextEntryFieldModel.swift b/MVMCoreUI/Atomic/Atoms/TextFields/TextEntryFieldModel.swift index c35d812e..761fa5b2 100644 --- a/MVMCoreUI/Atomic/Atoms/TextFields/TextEntryFieldModel.swift +++ b/MVMCoreUI/Atomic/Atoms/TextFields/TextEntryFieldModel.swift @@ -13,6 +13,7 @@ case password case number case email + case text } //-------------------------------------------------- diff --git a/MVMCoreUI/BaseClasses/TextView.swift b/MVMCoreUI/BaseClasses/TextView.swift index 23117718..ff18128f 100644 --- a/MVMCoreUI/BaseClasses/TextView.swift +++ b/MVMCoreUI/BaseClasses/TextView.swift @@ -9,12 +9,6 @@ import UIKit -@objc protocol MFTextViewDelegate { - // Dismisses the keyboard. - @objc optional func dismissFieldInput(_ sender: Any?) -} - - @objc open class TextView: UITextView, UITextViewDelegate { //-------------------------------------------------- // MARK: - Properties @@ -27,17 +21,9 @@ import UIKit /// Set to true to hide the blinking textField cursor. public var hideBlinkingCaret = false - /// Hides the custom drawn border. - public var hideBorder = true - - private var borderPath: UIBezierPath? - - /// If true - public var hasError = false - weak var bottomLine: SeparatorView? - public var placeholderFont: UIFont = MFStyler.fontB3()! { + public var placeholderFont: UIFont = MFStyler.fontRegularMicro() { didSet { if text.isEmpty { font = placeholderFont @@ -89,7 +75,7 @@ import UIKit /// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well. public weak var didDeleteDelegate: TextFieldDidDeleteProtocol? - /// If you're using a MFViewController, 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 { delegate = newValue } @@ -129,7 +115,7 @@ import UIKit public func initialSetup() { if !initialSetupPerformed { - tintColor = .black + tintColor = .mvmBlack initialSetupPerformed = true setupView() } @@ -150,47 +136,13 @@ import UIKit didDeleteDelegate?.textFieldDidDelete() } - //-------------------------------------------------- - // MARK: - Drawing - //-------------------------------------------------- - - open override func draw(_ rect: CGRect) { - super.draw(rect) - - borderPath?.removeAllPoints() - - if !hideBorder { - layer.cornerRadius = 0 - borderPath = UIBezierPath() - let width = frame.origin.x + frame.size.width - let height = frame.origin.y + frame.size.height - - borderPath?.move(to: CGPoint(x: frame.origin.x, y: height)) - borderPath?.addLine(to: CGPoint(x: frame.origin.x, y: frame.origin.y)) - borderPath?.addLine(to: CGPoint(x: width, y: frame.origin.y)) - borderPath?.addLine(to: CGPoint(x: width, y: height)) - borderPath?.lineWidth = 1 - - var strokeColor: UIColor? - - if hasError { - strokeColor = .mvmOrangeAA - bottomLine?.backgroundColor = .mvmOrangeAA - } else { - strokeColor = .mvmCoolGray3 - bottomLine?.backgroundColor = .mvmBlack - } - strokeColor?.setStroke() - borderPath?.stroke() - } - } - //-------------------------------------------------- // MARK: - Observing Methods //-------------------------------------------------- /// Executes on UITextView.textDidEndEditingNotification @objc open func dismissFieldInput() { + resignFirstResponder() } @@ -216,6 +168,7 @@ import UIKit } @objc public func textViewShouldEndEditing(_ textView: UITextView) -> Bool { + return delegate?.textViewShouldEndEditing?(textView) ?? true } @@ -232,19 +185,19 @@ extension TextView: MVMCoreViewProtocol { /// Will be called only once. open func setupView() { + translatesAutoresizingMaskIntoConstraints = false insetsLayoutMarginsFromSafeArea = false + showsVerticalScrollIndicator = false + showsHorizontalScrollIndicator = false clipsToBounds = true - hasError = false - hideBorder = false - layer.cornerRadius = CGFloat(CornerRadiusLarge) - // Disable SmartQuotes smartQuotesType = .no smartDashesType = .no smartInsertDeleteType = .no font = textFont - MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegate) - textContainerInset = UIEdgeInsets(top: PaddingTwo, left: PaddingTwo, bottom: PaddingTwo, right: PaddingOne) + layer.borderWidth = 1 + layer.borderColor = UIColor.mvmBlack.cgColor + isEditable = true } } @@ -265,12 +218,18 @@ extension TextView: MoleculeViewProtocol { heightConstraint?.isActive = true } + isEditable = model.isEditable + textAlignment = model.textAlignment + textColor = model.textColor.uiColor text = model.text - placeholder = model.placeholder ?? "" - MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegate) + placeholder = model.placeholder + uiTextViewDelegate = delegateObject?.uiTextViewDelegate + MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegateObject?.uiTextViewDelegate) } open func reset() { + + setNeedsDisplay() backgroundColor = .clear text = "" } diff --git a/MVMCoreUI/BaseClasses/TextViewModel.swift b/MVMCoreUI/BaseClasses/TextViewModel.swift index 655b6933..1f7f70a7 100644 --- a/MVMCoreUI/BaseClasses/TextViewModel.swift +++ b/MVMCoreUI/BaseClasses/TextViewModel.swift @@ -18,11 +18,12 @@ open class TextViewModel: MoleculeModelProtocol { public var backgroundColor: Color? public var text: String = "" public var accessibilityText: String? - public var textColor: Color? + public var textColor: Color = Color(uiColor: .mvmBlack) public var fontStyle: LabelModel.FontStyle? - public var textAlignment: NSTextAlignment? + public var textAlignment: NSTextAlignment = .left public var height: CGFloat? - public var placeholder: String? + public var placeholder: String = "" + public var placeholderFont: LabelModel.FontStyle = LabelModel.FontStyle.RegularMicro public var isEditable: Bool = true //-------------------------------------------------- @@ -36,12 +37,11 @@ open class TextViewModel: MoleculeModelProtocol { case textColor case backgroundColor case fontStyle - case fontName - case fontSize case textAlignment case attributes case height case placeholder + case placeholderFont case isEditable } @@ -59,32 +59,50 @@ open class TextViewModel: MoleculeModelProtocol { required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + if let text = try typeContainer.decodeIfPresent(String.self, forKey: .text) { self.text = text } - placeholder = try typeContainer.decodeIfPresent(String.self, forKey: .text) - accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText) - textColor = try typeContainer.decodeIfPresent(Color.self, forKey: .textColor) - backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) - fontStyle = try typeContainer.decodeIfPresent(LabelModel.FontStyle.self, forKey: .fontStyle) - textAlignment = try typeContainer.decodeIfPresent(NSTextAlignment.self, forKey: .textAlignment) - height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height) + + if let placeholder = try typeContainer.decodeIfPresent(String.self, forKey: .placeholder) { + self.placeholder = placeholder + } + + if let placeholderFont = try typeContainer.decodeIfPresent(String.self, forKey: .placeholderFont), + let font = LabelModel.FontStyle(rawValue: placeholderFont) { + self.placeholderFont = font + } + + if let textAlignment = try typeContainer.decodeIfPresent(NSTextAlignment.self, forKey: .textAlignment) { + self.textAlignment = textAlignment + } + + if let textColor = try typeContainer.decodeIfPresent(Color.self, forKey: .textColor) { + self.textColor = textColor + } if let isEditable = try typeContainer.decodeIfPresent(Bool.self, forKey: .isEditable) { self.isEditable = isEditable } + + accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText) + backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) + fontStyle = try typeContainer.decodeIfPresent(LabelModel.FontStyle.self, forKey: .fontStyle) + height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encodeIfPresent(moleculeName, forKey: .moleculeName) try container.encode(text, forKey: .text) + try container.encode(placeholder, forKey: .placeholder) + try container.encode(placeholderFont, forKey: .placeholderFont) try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText) try container.encodeIfPresent(textColor, forKey: .textColor) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(fontStyle, forKey: .fontStyle) - try container.encodeIfPresent(textAlignment, forKey: .textAlignment) + try container.encode(textAlignment, forKey: .textAlignment) try container.encodeIfPresent(height, forKey: .height) - try container.encodeIfPresent(isEditable, forKey: .isEditable) + try container.encode(isEditable, forKey: .isEditable) } } diff --git a/MVMCoreUI/OtherHandlers/MVMCoreUIDelegateObject.swift b/MVMCoreUI/OtherHandlers/MVMCoreUIDelegateObject.swift index c9d5d34c..c63c4a87 100644 --- a/MVMCoreUI/OtherHandlers/MVMCoreUIDelegateObject.swift +++ b/MVMCoreUI/OtherHandlers/MVMCoreUIDelegateObject.swift @@ -8,10 +8,13 @@ import UIKit + open class MVMCoreUIDelegateObject: DelegateObject { + public weak var formHolderDelegate: FormHolderProtocol? public weak var buttonDelegate: ButtonDelegateProtocol? public weak var uiTextFieldDelegate: UITextFieldDelegate? + public weak var uiTextViewDelegate: UITextViewDelegate? public weak var observingTextFieldDelegate: ObservingTextFieldDelegate? public weak var moleculeDelegate: MoleculeDelegateProtocol? @@ -20,6 +23,7 @@ open class MVMCoreUIDelegateObject: DelegateObject { formHolderDelegate = delegate as? FormHolderProtocol buttonDelegate = delegate as? ButtonDelegateProtocol uiTextFieldDelegate = delegate as? UITextFieldDelegate + uiTextViewDelegate = delegate as? UITextViewDelegate observingTextFieldDelegate = delegate as? ObservingTextFieldDelegate moleculeDelegate = delegate as? MoleculeDelegateProtocol } @@ -28,4 +32,3 @@ open class MVMCoreUIDelegateObject: DelegateObject { return controller?.delegateObject?() as? MVMCoreUIDelegateObject } } -