textView functioning

This commit is contained in:
Kevin G Christiano 2020-04-10 10:40:05 -04:00
parent 891e78eaf1
commit 66374f85ce
4 changed files with 68 additions and 71 deletions

View File

@ -13,7 +13,7 @@ import UIKit
} }
@objcMembers open class DigitBox: EntryFieldContainer, UITextFieldDelegate, TextFieldDidDeleteProtocol { @objcMembers open class DigitBox: EntryFieldContainer, UITextFieldDelegate, TextInputDidDeleteProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Outlets // MARK: - Outlets
//-------------------------------------------------- //--------------------------------------------------

View File

@ -8,7 +8,7 @@
import UIKit import UIKit
public protocol TextFieldDidDeleteProtocol: class { public protocol TextInputDidDeleteProtocol: class {
func textFieldDidDelete() func textFieldDidDelete()
} }
@ -17,6 +17,7 @@ public protocol TextFieldDidDeleteProtocol: class {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
private var initialSetupPerformed = false private var initialSetupPerformed = false
/// Set to true to hide the blinking textField cursor. /// 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. /// 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 // MARK: - Initialization

View File

@ -18,30 +18,27 @@ import UIKit
private var initialSetupPerformed = false private var initialSetupPerformed = false
/// Set this property if you want updateView to update the font based on this standard and the size passed in. /// If true then text textView is currently displaying the stored placeholder text as there is not content to display.
public var standardFontSize: CGFloat = 0.0
public var isShowingPlaceholder = true public var isShowingPlaceholder = true
/// Set to true to hide the blinking textField cursor. /// Set to true to hide the blinking textField cursor.
public var hideBlinkingCaret = false public var hideBlinkingCaret = false
private var textTraits: (color: UIColor, font: UIFont) = (color: .mvmBlack, public var textViewModel: TextViewModel? {
font: Styler.Font.RegularBodySmall.getFont()) return model as? TextViewModel
private var placeholderTraits: (text: String, color: UIColor, font: UIFont) = (text: "", }
color: .mvmCoolGray3, font: Styler.Font.RegularMicro.getFont())
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Delegate // MARK: - Delegate
//-------------------------------------------------- //--------------------------------------------------
/// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well. /// 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. /// Holds a reference to the delegating class so this class can internally influence the TextField behavior as well.
private weak var proprietorTextDelegate: UITextViewDelegate? 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? { public weak var uiTextViewDelegate: UITextViewDelegate? {
get { return delegate } get { return delegate }
set { set {
@ -74,12 +71,15 @@ import UIKit
initialSetup() initialSetup()
} }
convenience init(placeholderText: String, delegate: UITextViewDelegate?) { convenience init(delegate: UITextViewDelegate) {
self.init(frame: .zero, textContainer: nil) self.init(frame: .zero, textContainer: nil)
self.delegate = delegate self.delegate = delegate
self.placeholderTraits.text = placeholderText
} }
//--------------------------------------------------
// MARK: - Setup
//--------------------------------------------------
public func initialSetup() { public func initialSetup() {
if !initialSetupPerformed { 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 { open override func caretRect(for position: UITextPosition) -> CGRect {
if hideBlinkingCaret { if hideBlinkingCaret {
@ -104,26 +109,40 @@ import UIKit
didDeleteDelegate?.textFieldDidDelete() didDeleteDelegate?.textFieldDidDelete()
} }
open func setTextViewState() { public func setTextAppearance() {
if isShowingPlaceholder { if isShowingPlaceholder {
text = "" setTextContentTraits()
font = textTraits.font
textColor = textTraits.color
isShowingPlaceholder = false
} else if text.isEmpty {
isShowingPlaceholder = true
textColor = placeholderTraits.color
text = placeholderTraits.text
font = placeholderTraits.font
} }
} }
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 // MARK: - UITextViewDelegate
//-------------------------------------------------- //--------------------------------------------------
//#pragma Mark UITextView Delegate methods forwarding
@objc public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { @objc public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
return proprietorTextDelegate?.textViewShouldBeginEditing?(textView) ?? true return proprietorTextDelegate?.textViewShouldBeginEditing?(textView) ?? true
@ -131,6 +150,7 @@ import UIKit
@objc public func textViewDidBeginEditing(_ textView: UITextView) { @objc public func textViewDidBeginEditing(_ textView: UITextView) {
setTextAppearance()
proprietorTextDelegate?.textViewDidBeginEditing?(textView) proprietorTextDelegate?.textViewDidBeginEditing?(textView)
} }
@ -141,8 +161,6 @@ import UIKit
@objc public func textViewDidChange(_ textView: UITextView) { @objc public func textViewDidChange(_ textView: UITextView) {
setTextViewState()
proprietorTextDelegate?.textViewDidChange?(textView) proprietorTextDelegate?.textViewDidChange?(textView)
} }
@ -153,6 +171,7 @@ import UIKit
@objc public func textViewDidEndEditing(_ textView: UITextView) { @objc public func textViewDidEndEditing(_ textView: UITextView) {
setPlaceholderIfAvailable()
proprietorTextDelegate?.textViewDidEndEditing?(textView) proprietorTextDelegate?.textViewDidEndEditing?(textView)
} }
} }
@ -174,7 +193,7 @@ extension TextView: MVMCoreViewProtocol {
smartQuotesType = .no smartQuotesType = .no
smartDashesType = .no smartDashesType = .no
smartInsertDeleteType = .no smartInsertDeleteType = .no
font = textTraits.font font = textViewModel?.fontStyle.getFont()
layer.borderWidth = 1 layer.borderWidth = 1
layer.borderColor = UIColor.mvmBlack.cgColor layer.borderColor = UIColor.mvmBlack.cgColor
isEditable = true isEditable = true
@ -193,10 +212,8 @@ extension TextView: MoleculeViewProtocol {
guard let model = model as? TextViewModel else { return } guard let model = model as? TextViewModel else { return }
if let height = model.height { heightConstraint = heightAnchor.constraint(equalToConstant: model.height)
heightConstraint = heightAnchor.constraint(equalToConstant: height) heightConstraint?.isActive = true
heightConstraint?.isActive = true
}
isEditable = model.isEditable isEditable = model.isEditable
textAlignment = model.textAlignment textAlignment = model.textAlignment
@ -204,32 +221,15 @@ extension TextView: MoleculeViewProtocol {
layer.borderColor = model.borderColor?.cgColor layer.borderColor = model.borderColor?.cgColor
layer.borderWidth = model.borderWidth layer.borderWidth = model.borderWidth
text = model.text text = model.text
isShowingPlaceholder = model.text.isEmpty
placeholderTraits.font = model.placeholderFont.getFont()
placeholderTraits.text = model.placeholder
uiTextViewDelegate = delegateObject?.uiTextViewDelegate uiTextViewDelegate = delegateObject?.uiTextViewDelegate
isShowingPlaceholder = model.text.isEmpty
if let accessibilityText = model.accessibilityText { if let accessibilityText = model.accessibilityText {
accessibilityLabel = accessibilityText accessibilityLabel = accessibilityText
} }
font = model.fontStyle.getFont() font = model.fontStyle.getFont()
standardFontSize = model.fontStyle.pointSize() setPlaceholderIfAvailable()
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!
}
if isEditable { if isEditable {
MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegateObject?.uiTextViewDelegate) MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegateObject?.uiTextViewDelegate)
@ -238,8 +238,10 @@ extension TextView: MoleculeViewProtocol {
open func reset() { open func reset() {
setNeedsDisplay()
backgroundColor = .clear backgroundColor = .clear
text = "" text = ""
inputAccessoryView?.removeFromSuperview()
layer.borderColor = UIColor.mvmBlack.cgColor
layer.borderWidth = 0
} }
} }

View File

@ -16,20 +16,19 @@ open class TextViewModel: MoleculeModelProtocol {
public static var identifier: String = "textView" public static var identifier: String = "textView"
public var backgroundColor: Color? public var backgroundColor: Color?
public var text: String = ""
public var accessibilityText: String? public var accessibilityText: String?
public var text: String = ""
public var textColor: Color = Color(uiColor: .mvmBlack) 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 fontStyle: Styler.Font = Styler.Font.RegularBodySmall
public var textAlignment: NSTextAlignment = .left public var textAlignment: NSTextAlignment = .left
public var height: CGFloat? public var height: CGFloat
public var placeholder: String = "" 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 isEditable: Bool = true
public var borderColor: Color? public var borderColor: Color?
public var borderWidth: CGFloat = 0 public var borderWidth: CGFloat = 0
public var showsPlaceholder: Bool = true
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Keys // MARK: - Keys
@ -42,15 +41,13 @@ open class TextViewModel: MoleculeModelProtocol {
case accessibilityText case accessibilityText
case textColor case textColor
case fontStyle case fontStyle
case fontSize
case fontName
case textAlignment case textAlignment
case attributes case attributes
case height case height
case borderColor case borderColor
case borderWidth case borderWidth
case placeholder case placeholder
case placeholderFont case placeholderFontStyle
case isEditable case isEditable
} }
@ -58,7 +55,8 @@ open class TextViewModel: MoleculeModelProtocol {
// MARK: - Initializer // MARK: - Initializer
//-------------------------------------------------- //--------------------------------------------------
public init(text: String) { public init(height: CGFloat, text: String = "") {
self.height = height
self.text = text self.text = text
} }
@ -77,8 +75,8 @@ open class TextViewModel: MoleculeModelProtocol {
self.placeholder = placeholder self.placeholder = placeholder
} }
if let placeholderFont = try typeContainer.decodeIfPresent(Styler.Font.self, forKey: .placeholderFont) { if let placeholderFontStyle = try typeContainer.decodeIfPresent(Styler.Font.self, forKey: .placeholderFontStyle) {
self.placeholderFont = placeholderFont self.placeholderFontStyle = placeholderFontStyle
} }
if let textAlignment = try typeContainer.decodeIfPresent(NSTextAlignment.self, forKey: .textAlignment) { 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) borderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .borderColor)
accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText) accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
fontSize = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .fontSize) height = try typeContainer.decode(CGFloat.self, forKey: .height)
fontName = try typeContainer.decodeIfPresent(String.self, forKey: .fontName)
height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height)
} }
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {
@ -116,13 +112,11 @@ open class TextViewModel: MoleculeModelProtocol {
try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText) try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
try container.encodeIfPresent(textColor, forKey: .textColor) try container.encodeIfPresent(textColor, forKey: .textColor)
try container.encodeIfPresent(fontStyle, forKey: .fontStyle) try container.encodeIfPresent(fontStyle, forKey: .fontStyle)
try container.encodeIfPresent(height, forKey: .height)
try container.encodeIfPresent(borderColor, forKey: .borderColor) try container.encodeIfPresent(borderColor, forKey: .borderColor)
try container.encodeIfPresent(fontSize, forKey: .fontSize) try container.encode(height, forKey: .height)
try container.encodeIfPresent(fontName, forKey: .fontName)
try container.encode(text, forKey: .text) try container.encode(text, forKey: .text)
try container.encode(placeholder, forKey: .placeholder) 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(textAlignment, forKey: .textAlignment)
try container.encode(isEditable, forKey: .isEditable) try container.encode(isEditable, forKey: .isEditable)
try container.encode(borderWidth, forKey: .borderWidth) try container.encode(borderWidth, forKey: .borderWidth)