current textview state

This commit is contained in:
Kevin G Christiano 2020-04-08 16:21:46 -04:00
parent 8141fa934f
commit 6e96be982b
2 changed files with 74 additions and 39 deletions

View File

@ -21,7 +21,8 @@ import UIKit
/// Set to true to hide the blinking textField cursor.
public var hideBlinkingCaret = false
weak var bottomLine: SeparatorView?
private var textTraits: (color: UIColor, font: UIFont) = (color: .mvmBlack, font: MFStyler.fontRegularBodySmall())
private var placeholderTraits: (color: UIColor, font: UIFont) = (color: .mvmCoolGray3, font: MFStyler.fontRegularMicro() )
public var placeholderFont: UIFont = MFStyler.fontRegularMicro() {
didSet {
@ -41,32 +42,19 @@ import UIKit
public var placeholderTextColor: UIColor = .mvmCoolGray3
public var displaysPlaceholder = false
public var showsPlaceholder = true
private(set) var textStore: String = ""
public override var text: String! {
public var isShowingPlaceholder = true {
didSet {
if displaysPlaceholder && text.isEmpty {
textColor = isShowingPlaceholder ? placeholderTextColor : .mvmBlack
font = isShowingPlaceholder ? placeholderFont : textFont
if isShowingPlaceholder {
text = placeholder
textColor = placeholderTextColor
} else {
textColor = .mvmBlack
}
}
}
private(set) var textFont: UIFont = MFStyler.fontB2()!
public override var font: UIFont? {
didSet {
if displaysPlaceholder && text.isEmpty {
font = placeholderFont
} else {
textColor = .mvmBlack
}
}
}
private(set) var textFont: UIFont = MFStyler.fontRegularBodySmall()
//--------------------------------------------------
// MARK: - Delegate
@ -75,10 +63,16 @@ 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?
/// 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 = newValue }
set {
delegate = self
proprietorTextDelegate = newValue
}
}
//--------------------------------------------------
@ -108,7 +102,7 @@ import UIKit
convenience init(placeholderText: String, delegate: UITextViewDelegate?) {
self.init(frame: .zero, textContainer: nil)
self.delegate = delegate
displaysPlaceholder = true
showsPlaceholder = true
self.placeholder = placeholderText
}
@ -134,10 +128,17 @@ import UIKit
open override func deleteBackward() {
super.deleteBackward()
didDeleteDelegate?.textFieldDidDelete()
if isShowingPlaceholder {
text = ""
isShowingPlaceholder = false
} else if text.isEmpty {
isShowingPlaceholder = true
}
}
//--------------------------------------------------
// MARK: - Observing Methods
// MARK: - UITextViewDelegate
//--------------------------------------------------
/// Executes on UITextView.textDidEndEditingNotification
@ -149,32 +150,32 @@ import UIKit
//#pragma Mark UITextView Delegate methods forwarding
@objc public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
return delegate?.textViewShouldBeginEditing?(textView) ?? true
return proprietorTextDelegate?.textViewShouldBeginEditing?(textView) ?? true
}
@objc public func textViewDidBeginEditing(_ textView: UITextView) {
delegate?.textViewDidBeginEditing?(textView)
proprietorTextDelegate?.textViewDidBeginEditing?(textView)
}
@objc public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return delegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text) ?? true
return proprietorTextDelegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text) ?? true
}
@objc public func textViewDidChange(_ textView: UITextView) {
delegate?.textViewDidChange?(textView)
proprietorTextDelegate?.textViewDidChange?(textView)
}
@objc public func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
return delegate?.textViewShouldEndEditing?(textView) ?? true
return proprietorTextDelegate?.textViewShouldEndEditing?(textView) ?? true
}
@objc public func textViewDidEndEditing(_ textView: UITextView) {
delegate?.textViewDidEndEditing?(textView)
proprietorTextDelegate?.textViewDidEndEditing?(textView)
}
}
@ -190,6 +191,7 @@ extension TextView: MVMCoreViewProtocol {
insetsLayoutMarginsFromSafeArea = false
showsVerticalScrollIndicator = false
showsHorizontalScrollIndicator = false
backgroundColor = .clear
clipsToBounds = true
smartQuotesType = .no
smartDashesType = .no
@ -221,10 +223,24 @@ extension TextView: MoleculeViewProtocol {
isEditable = model.isEditable
textAlignment = model.textAlignment
textColor = model.textColor.uiColor
layer.borderColor = model.borderColor?.cgColor
layer.borderWidth = model.borderWidth
// font = model.fontStyle
text = model.text
isShowingPlaceholder = model.text.isEmpty
placeholder = model.placeholder
// placeholderFont = model.placeholderFont
uiTextViewDelegate = delegateObject?.uiTextViewDelegate
MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegateObject?.uiTextViewDelegate)
if let accessibilityText = model.accessibilityText {
accessibilityLabel = accessibilityText
}
if isEditable {
MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegateObject?.uiTextViewDelegate)
}
}
open func reset() {

View File

@ -19,12 +19,17 @@ open class TextViewModel: MoleculeModelProtocol {
public var text: String = ""
public var accessibilityText: String?
public var textColor: Color = Color(uiColor: .mvmBlack)
public var fontSize: CGFloat?
public var fontName: String?
public var fontStyle: LabelModel.FontStyle?
public var textAlignment: NSTextAlignment = .left
public var height: CGFloat?
public var placeholder: String = ""
public var placeholderFont: LabelModel.FontStyle = LabelModel.FontStyle.RegularMicro
public var isEditable: Bool = true
public var borderColor: Color?
public var borderWidth: CGFloat = 0
public var showsPlaceholder: Bool = true
//--------------------------------------------------
// MARK: - Keys
@ -32,14 +37,18 @@ open class TextViewModel: MoleculeModelProtocol {
private enum CodingKeys: String, CodingKey {
case moleculeName
case backgroundColor
case text
case accessibilityText
case textColor
case backgroundColor
case fontStyle
case fontSize
case fontName
case textAlignment
case attributes
case height
case borderColor
case borderWidth
case placeholder
case placeholderFont
case isEditable
@ -68,9 +77,8 @@ open class TextViewModel: MoleculeModelProtocol {
self.placeholder = placeholder
}
if let placeholderFont = try typeContainer.decodeIfPresent(String.self, forKey: .placeholderFont),
let font = LabelModel.FontStyle(rawValue: placeholderFont) {
self.placeholderFont = font
if let placeholderFont = try typeContainer.decodeIfPresent(LabelModel.FontStyle.self, forKey: .placeholderFont) {
self.placeholderFont = placeholderFont
}
if let textAlignment = try typeContainer.decodeIfPresent(NSTextAlignment.self, forKey: .textAlignment) {
@ -85,8 +93,15 @@ open class TextViewModel: MoleculeModelProtocol {
self.isEditable = isEditable
}
if let borderWidth = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .borderWidth) {
self.borderWidth = borderWidth
}
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: .accessibilityText)
fontName = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText)
fontStyle = try typeContainer.decodeIfPresent(LabelModel.FontStyle.self, forKey: .fontStyle)
height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height)
}
@ -94,15 +109,19 @@ open class TextViewModel: MoleculeModelProtocol {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(moleculeName, forKey: .moleculeName)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
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(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.encode(textAlignment, forKey: .textAlignment)
try container.encodeIfPresent(height, forKey: .height)
try container.encode(isEditable, forKey: .isEditable)
try container.encode(borderWidth, forKey: .borderWidth)
}
}