From 66c638a1b42145deb6b0e92040d5503d16d608ef Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Fri, 8 May 2020 12:20:06 -0400 Subject: [PATCH] general working order --- .../Atoms/TextFields/TextViewEntryField.swift | 50 +++++++++++++++-- MVMCoreUI/BaseClasses/TextView.swift | 53 +------------------ 2 files changed, 47 insertions(+), 56 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/TextFields/TextViewEntryField.swift b/MVMCoreUI/Atomic/Atoms/TextFields/TextViewEntryField.swift index 56a62629..e2caeba9 100644 --- a/MVMCoreUI/Atomic/Atoms/TextFields/TextViewEntryField.swift +++ b/MVMCoreUI/Atomic/Atoms/TextFields/TextViewEntryField.swift @@ -96,6 +96,9 @@ class TextViewEntryField: EntryField, UITextViewDelegate, ObservingTextFieldDele // MARK: - Delegate Properties //-------------------------------------------------- + /// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well. + private weak var proprietorTextDelegate: UITextViewDelegate? + /// The delegate and block for validation. Validates if the text that the user has entered. public weak var observingTextFieldDelegate: ObservingTextFieldDelegate? { didSet { @@ -117,7 +120,10 @@ class TextViewEntryField: EntryField, UITextViewDelegate, ObservingTextFieldDele /// If you're using a ViewController, you must set this to it public weak var uiTextViewDelegate: UITextViewDelegate? { get { return textView.delegate } - set { textView.delegate = newValue } + set { + textView.delegate = self + proprietorTextDelegate = newValue + } } @objc public func setBothTextDelegates(to delegate: (UITextViewDelegate & ObservingTextFieldDelegate)?) { @@ -128,7 +134,7 @@ class TextViewEntryField: EntryField, UITextViewDelegate, ObservingTextFieldDele open func setupTextViewToolbar() { let observingDelegate = observingTextFieldDelegate ?? self textView.inputAccessoryView = UIToolbar.getToolbarWithDoneButton(delegate: observingDelegate, - action: #selector(observingDelegate.dismissFieldInput)) + action: #selector(observingDelegate.dismissFieldInput)) } //-------------------------------------------------- @@ -137,7 +143,6 @@ class TextViewEntryField: EntryField, UITextViewDelegate, ObservingTextFieldDele @objc open override func setupFieldContainerContent(_ container: UIView) { - textView.delegate = self container.addSubview(textView) NSLayoutConstraint.activate([ @@ -176,7 +181,6 @@ class TextViewEntryField: EntryField, UITextViewDelegate, ObservingTextFieldDele /// Executes on UITextField.textDidBeginEditingNotification @objc func startEditing() { - isSelected = true textView.becomeFirstResponder() } @@ -206,6 +210,44 @@ class TextViewEntryField: EntryField, UITextViewDelegate, ObservingTextFieldDele resignFirstResponder() } + //-------------------------------------------------- + // MARK: - UITextViewDelegate + //-------------------------------------------------- + + @objc public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { + + return proprietorTextDelegate?.textViewShouldBeginEditing?(textView) ?? true + } + + @objc public func textViewDidBeginEditing(_ textView: UITextView) { + + self.textView.setTextAppearance() + isSelected = true + proprietorTextDelegate?.textViewDidBeginEditing?(textView) + } + + @objc public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { + + return proprietorTextDelegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text) ?? true + } + + @objc public func textViewDidChange(_ textView: UITextView) { + + proprietorTextDelegate?.textViewDidChange?(textView) + } + + @objc public func textViewShouldEndEditing(_ textView: UITextView) -> Bool { + + return proprietorTextDelegate?.textViewShouldEndEditing?(textView) ?? true + } + + @objc public func textViewDidEndEditing(_ textView: UITextView) { + + self.textView.setPlaceholderIfAvailable() + isSelected = false + proprietorTextDelegate?.textViewDidEndEditing?(textView) + } + //-------------------------------------------------- // MARK: - MoleculeViewProtocol //-------------------------------------------------- diff --git a/MVMCoreUI/BaseClasses/TextView.swift b/MVMCoreUI/BaseClasses/TextView.swift index 3ce027b5..208e69bc 100644 --- a/MVMCoreUI/BaseClasses/TextView.swift +++ b/MVMCoreUI/BaseClasses/TextView.swift @@ -30,9 +30,7 @@ import UIKit public var placeholderTextColor: UIColor = .mvmCoolGray3 public var isEnabled: Bool = true { - didSet { - isUserInteractionEnabled = isEnabled - } + didSet { isUserInteractionEnabled = isEnabled } } //-------------------------------------------------- @@ -42,18 +40,6 @@ import UIKit /// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well. 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. - public weak var uiTextViewDelegate: UITextViewDelegate? { - get { return delegate } - set { - delegate = self - proprietorTextDelegate = newValue - } - } - var delegateObject: MVMCoreUIDelegateObject? //-------------------------------------------------- @@ -116,7 +102,6 @@ import UIKit showsVerticalScrollIndicator = false showsHorizontalScrollIndicator = false isSecureTextEntry = false -// textContainerInset = UIEdgeInsets(top: Padding.Three, left: Padding.Three, bottom: Padding.Three, right: Padding.Three) backgroundColor = .mvmWhite clipsToBounds = true smartQuotesType = .no @@ -201,40 +186,4 @@ import UIKit resignFirstResponder() } - - //-------------------------------------------------- - // MARK: - UITextViewDelegate - //-------------------------------------------------- - - @objc public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { - - return proprietorTextDelegate?.textViewShouldBeginEditing?(textView) ?? true - } - - @objc public func textViewDidBeginEditing(_ textView: UITextView) { - - setTextAppearance() - proprietorTextDelegate?.textViewDidBeginEditing?(textView) - } - - @objc public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { - - return proprietorTextDelegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text) ?? true - } - - @objc public func textViewDidChange(_ textView: UITextView) { - - proprietorTextDelegate?.textViewDidChange?(textView) - } - - @objc public func textViewShouldEndEditing(_ textView: UITextView) -> Bool { - - return proprietorTextDelegate?.textViewShouldEndEditing?(textView) ?? true - } - - @objc public func textViewDidEndEditing(_ textView: UITextView) { - - setPlaceholderIfAvailable() - proprietorTextDelegate?.textViewDidEndEditing?(textView) - } }