latest textview

This commit is contained in:
Kevin G Christiano 2020-04-08 13:53:53 -04:00
parent be61cba8e8
commit 8141fa934f
5 changed files with 57 additions and 76 deletions

View File

@ -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? { public weak var uiTextFieldDelegate: UITextFieldDelegate? {
get { return textField.delegate } get { return textField.delegate }
set { textField.delegate = newValue } set { textField.delegate = newValue }

View File

@ -13,6 +13,7 @@
case password case password
case number case number
case email case email
case text
} }
//-------------------------------------------------- //--------------------------------------------------

View File

@ -9,12 +9,6 @@
import UIKit import UIKit
@objc protocol MFTextViewDelegate {
// Dismisses the keyboard.
@objc optional func dismissFieldInput(_ sender: Any?)
}
@objc open class TextView: UITextView, UITextViewDelegate { @objc open class TextView: UITextView, UITextViewDelegate {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
@ -27,17 +21,9 @@ import UIKit
/// 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
/// Hides the custom drawn border.
public var hideBorder = true
private var borderPath: UIBezierPath?
/// If true
public var hasError = false
weak var bottomLine: SeparatorView? weak var bottomLine: SeparatorView?
public var placeholderFont: UIFont = MFStyler.fontB3()! { public var placeholderFont: UIFont = MFStyler.fontRegularMicro() {
didSet { didSet {
if text.isEmpty { if text.isEmpty {
font = placeholderFont 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. /// 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: 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? { public weak var uiTextViewDelegate: UITextViewDelegate? {
get { return delegate } get { return delegate }
set { delegate = newValue } set { delegate = newValue }
@ -129,7 +115,7 @@ import UIKit
public func initialSetup() { public func initialSetup() {
if !initialSetupPerformed { if !initialSetupPerformed {
tintColor = .black tintColor = .mvmBlack
initialSetupPerformed = true initialSetupPerformed = true
setupView() setupView()
} }
@ -150,47 +136,13 @@ import UIKit
didDeleteDelegate?.textFieldDidDelete() 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 // MARK: - Observing Methods
//-------------------------------------------------- //--------------------------------------------------
/// Executes on UITextView.textDidEndEditingNotification /// Executes on UITextView.textDidEndEditingNotification
@objc open func dismissFieldInput() { @objc open func dismissFieldInput() {
resignFirstResponder() resignFirstResponder()
} }
@ -216,6 +168,7 @@ import UIKit
} }
@objc public func textViewShouldEndEditing(_ textView: UITextView) -> Bool { @objc public func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
return delegate?.textViewShouldEndEditing?(textView) ?? true return delegate?.textViewShouldEndEditing?(textView) ?? true
} }
@ -232,19 +185,19 @@ extension TextView: MVMCoreViewProtocol {
/// Will be called only once. /// Will be called only once.
open func setupView() { open func setupView() {
translatesAutoresizingMaskIntoConstraints = false translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false insetsLayoutMarginsFromSafeArea = false
showsVerticalScrollIndicator = false
showsHorizontalScrollIndicator = false
clipsToBounds = true clipsToBounds = true
hasError = false
hideBorder = false
layer.cornerRadius = CGFloat(CornerRadiusLarge)
// Disable SmartQuotes
smartQuotesType = .no smartQuotesType = .no
smartDashesType = .no smartDashesType = .no
smartInsertDeleteType = .no smartInsertDeleteType = .no
font = textFont font = textFont
MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegate) layer.borderWidth = 1
textContainerInset = UIEdgeInsets(top: PaddingTwo, left: PaddingTwo, bottom: PaddingTwo, right: PaddingOne) layer.borderColor = UIColor.mvmBlack.cgColor
isEditable = true
} }
} }
@ -265,12 +218,18 @@ extension TextView: MoleculeViewProtocol {
heightConstraint?.isActive = true heightConstraint?.isActive = true
} }
isEditable = model.isEditable
textAlignment = model.textAlignment
textColor = model.textColor.uiColor
text = model.text text = model.text
placeholder = model.placeholder ?? "" placeholder = model.placeholder
MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegate) uiTextViewDelegate = delegateObject?.uiTextViewDelegate
MVMCoreUICommonViewsUtility.addDismissToolbar(to: self, delegate: delegateObject?.uiTextViewDelegate)
} }
open func reset() { open func reset() {
setNeedsDisplay()
backgroundColor = .clear backgroundColor = .clear
text = "" text = ""
} }

View File

@ -18,11 +18,12 @@ open class TextViewModel: MoleculeModelProtocol {
public var backgroundColor: Color? public var backgroundColor: Color?
public var text: String = "" public var text: String = ""
public var accessibilityText: String? public var accessibilityText: String?
public var textColor: Color? public var textColor: Color = Color(uiColor: .mvmBlack)
public var fontStyle: LabelModel.FontStyle? public var fontStyle: LabelModel.FontStyle?
public var textAlignment: NSTextAlignment? public var textAlignment: NSTextAlignment = .left
public var height: CGFloat? 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 public var isEditable: Bool = true
//-------------------------------------------------- //--------------------------------------------------
@ -36,12 +37,11 @@ open class TextViewModel: MoleculeModelProtocol {
case textColor case textColor
case backgroundColor case backgroundColor
case fontStyle case fontStyle
case fontName
case fontSize
case textAlignment case textAlignment
case attributes case attributes
case height case height
case placeholder case placeholder
case placeholderFont
case isEditable case isEditable
} }
@ -59,32 +59,50 @@ open class TextViewModel: MoleculeModelProtocol {
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self) let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
if let text = try typeContainer.decodeIfPresent(String.self, forKey: .text) { if let text = try typeContainer.decodeIfPresent(String.self, forKey: .text) {
self.text = text self.text = text
} }
placeholder = try typeContainer.decodeIfPresent(String.self, forKey: .text)
accessibilityText = try typeContainer.decodeIfPresent(String.self, forKey: .accessibilityText) if let placeholder = try typeContainer.decodeIfPresent(String.self, forKey: .placeholder) {
textColor = try typeContainer.decodeIfPresent(Color.self, forKey: .textColor) self.placeholder = placeholder
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) if let placeholderFont = try typeContainer.decodeIfPresent(String.self, forKey: .placeholderFont),
height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height) 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) { if let isEditable = try typeContainer.decodeIfPresent(Bool.self, forKey: .isEditable) {
self.isEditable = 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 { public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self) var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(moleculeName, forKey: .moleculeName) try container.encodeIfPresent(moleculeName, forKey: .moleculeName)
try container.encode(text, forKey: .text) 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(accessibilityText, forKey: .accessibilityText)
try container.encodeIfPresent(textColor, forKey: .textColor) try container.encodeIfPresent(textColor, forKey: .textColor)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(fontStyle, forKey: .fontStyle) 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(height, forKey: .height)
try container.encodeIfPresent(isEditable, forKey: .isEditable) try container.encode(isEditable, forKey: .isEditable)
} }
} }

View File

@ -8,10 +8,13 @@
import UIKit import UIKit
open class MVMCoreUIDelegateObject: DelegateObject { open class MVMCoreUIDelegateObject: DelegateObject {
public weak var formHolderDelegate: FormHolderProtocol? public weak var formHolderDelegate: FormHolderProtocol?
public weak var buttonDelegate: ButtonDelegateProtocol? public weak var buttonDelegate: ButtonDelegateProtocol?
public weak var uiTextFieldDelegate: UITextFieldDelegate? public weak var uiTextFieldDelegate: UITextFieldDelegate?
public weak var uiTextViewDelegate: UITextViewDelegate?
public weak var observingTextFieldDelegate: ObservingTextFieldDelegate? public weak var observingTextFieldDelegate: ObservingTextFieldDelegate?
public weak var moleculeDelegate: MoleculeDelegateProtocol? public weak var moleculeDelegate: MoleculeDelegateProtocol?
@ -20,6 +23,7 @@ open class MVMCoreUIDelegateObject: DelegateObject {
formHolderDelegate = delegate as? FormHolderProtocol formHolderDelegate = delegate as? FormHolderProtocol
buttonDelegate = delegate as? ButtonDelegateProtocol buttonDelegate = delegate as? ButtonDelegateProtocol
uiTextFieldDelegate = delegate as? UITextFieldDelegate uiTextFieldDelegate = delegate as? UITextFieldDelegate
uiTextViewDelegate = delegate as? UITextViewDelegate
observingTextFieldDelegate = delegate as? ObservingTextFieldDelegate observingTextFieldDelegate = delegate as? ObservingTextFieldDelegate
moleculeDelegate = delegate as? MoleculeDelegateProtocol moleculeDelegate = delegate as? MoleculeDelegateProtocol
} }
@ -28,4 +32,3 @@ open class MVMCoreUIDelegateObject: DelegateObject {
return controller?.delegateObject?() as? MVMCoreUIDelegateObject return controller?.delegateObject?() as? MVMCoreUIDelegateObject
} }
} }