Digital ACT191 story ONEAPP-6682 Checking character count overflow
This commit is contained in:
parent
783143717c
commit
e319153ac6
@ -35,7 +35,8 @@ open class TextArea: EntryFieldBase {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
internal var minWidthConstraint: NSLayoutConstraint?
|
internal var minWidthConstraint: NSLayoutConstraint?
|
||||||
internal var textViewHeightConstraint: NSLayoutConstraint?
|
internal var textViewHeightConstraint: NSLayoutConstraint?
|
||||||
|
internal var allowCharCount: Int = 0
|
||||||
|
|
||||||
internal var inputFieldStackView: UIStackView = {
|
internal var inputFieldStackView: UIStackView = {
|
||||||
return UIStackView().with {
|
return UIStackView().with {
|
||||||
$0.translatesAutoresizingMaskIntoConstraints = false
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
||||||
@ -64,10 +65,6 @@ open class TextArea: EntryFieldBase {
|
|||||||
$0.setContentCompressionResistancePriority(.required, for: .vertical)
|
$0.setContentCompressionResistancePriority(.required, for: .vertical)
|
||||||
$0.textStyle = .bodySmall
|
$0.textStyle = .bodySmall
|
||||||
}
|
}
|
||||||
open var maxLengthLabel = Label().with {
|
|
||||||
$0.setContentCompressionResistancePriority(.required, for: .vertical)
|
|
||||||
$0.textStyle = .bodySmall
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Public Properties
|
// MARK: - Public Properties
|
||||||
@ -96,9 +93,6 @@ open class TextArea: EntryFieldBase {
|
|||||||
super.setup()
|
super.setup()
|
||||||
minWidthConstraint = containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: containerSize.width)
|
minWidthConstraint = containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: containerSize.width)
|
||||||
minWidthConstraint?.isActive = true
|
minWidthConstraint?.isActive = true
|
||||||
characterCountLabel.text = "0"
|
|
||||||
maxLengthLabel.text = "/200"
|
|
||||||
|
|
||||||
controlContainerView.addSubview(textView)
|
controlContainerView.addSubview(textView)
|
||||||
textView
|
textView
|
||||||
.pinTop()
|
.pinTop()
|
||||||
@ -118,6 +112,8 @@ open class TextArea: EntryFieldBase {
|
|||||||
open override func reset() {
|
open override func reset() {
|
||||||
super.reset()
|
super.reset()
|
||||||
textView.text = ""
|
textView.text = ""
|
||||||
|
characterCountLabel.reset()
|
||||||
|
characterCountLabel.textStyle = .bodySmall
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Container for the area in which the user interacts.
|
/// Container for the area in which the user interacts.
|
||||||
@ -132,7 +128,6 @@ open class TextArea: EntryFieldBase {
|
|||||||
|
|
||||||
textView.isEditable = isEnabled
|
textView.isEditable = isEnabled
|
||||||
textView.textColor = textViewTextColorConfiguration.getColor(self)
|
textView.textColor = textViewTextColorConfiguration.getColor(self)
|
||||||
|
|
||||||
//set the width constraints
|
//set the width constraints
|
||||||
if let width {
|
if let width {
|
||||||
widthConstraint?.constant = width
|
widthConstraint?.constant = width
|
||||||
@ -143,6 +138,12 @@ open class TextArea: EntryFieldBase {
|
|||||||
widthConstraint?.isActive = false
|
widthConstraint?.isActive = false
|
||||||
minWidthConstraint?.isActive = true
|
minWidthConstraint?.isActive = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow - 20% of character limit
|
||||||
|
let overflowLimit = Double(maxLength ?? 0) * 0.20
|
||||||
|
allowCharCount = Int(overflowLimit) + (maxLength ?? 0)
|
||||||
|
characterCountLabel.text = getLimitText()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Container for the area which shows helper text, error text, character count, max length value.
|
/// Container for the area which shows helper text, error text, character count, max length value.
|
||||||
@ -151,9 +152,18 @@ open class TextArea: EntryFieldBase {
|
|||||||
bottomStackView.pinToSuperView()
|
bottomStackView.pinToSuperView()
|
||||||
bottomStackView.addArrangedSubview(bottomContainerView)
|
bottomStackView.addArrangedSubview(bottomContainerView)
|
||||||
bottomStackView.addArrangedSubview(characterCountLabel)
|
bottomStackView.addArrangedSubview(characterCountLabel)
|
||||||
bottomStackView.addArrangedSubview(maxLengthLabel)
|
|
||||||
return bottomView
|
return bottomView
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Private Methods
|
||||||
|
//--------------------------------------------------
|
||||||
|
private func getLimitText() -> String {
|
||||||
|
let count = textView.text.count
|
||||||
|
let countStr = (count > maxLength ?? 0) ? ("-" + "\(count-(maxLength ?? 0))") : "\(count)"
|
||||||
|
let text = "\(countStr)" + "/" + "\(maxLength ?? 0)"
|
||||||
|
return text
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension TextArea: UITextViewDelegate {
|
extension TextArea: UITextViewDelegate {
|
||||||
@ -177,9 +187,12 @@ extension TextArea: UITextViewDelegate {
|
|||||||
textViewHeightConstraint.isActive = true
|
textViewHeightConstraint.isActive = true
|
||||||
}
|
}
|
||||||
|
|
||||||
//setting the value and firing control event
|
if textView.text.count <= allowCharCount {
|
||||||
value = textView.text
|
//setting the value and firing control event
|
||||||
sendActions(for: .valueChanged)
|
value = textView.text
|
||||||
|
sendActions(for: .valueChanged)
|
||||||
|
} else {
|
||||||
|
textView.text.removeLast()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user