From e319153ac659f79f581ab4a939848b90f895a8d1 Mon Sep 17 00:00:00 2001 From: vasavk Date: Fri, 23 Feb 2024 16:11:14 +0530 Subject: [PATCH] Digital ACT191 story ONEAPP-6682 Checking character count overflow --- .../TextFields/TextArea/TextArea.swift | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/VDS/Components/TextFields/TextArea/TextArea.swift b/VDS/Components/TextFields/TextArea/TextArea.swift index 7cca53da..105286b5 100644 --- a/VDS/Components/TextFields/TextArea/TextArea.swift +++ b/VDS/Components/TextFields/TextArea/TextArea.swift @@ -35,7 +35,8 @@ open class TextArea: EntryFieldBase { //-------------------------------------------------- internal var minWidthConstraint: NSLayoutConstraint? internal var textViewHeightConstraint: NSLayoutConstraint? - + internal var allowCharCount: Int = 0 + internal var inputFieldStackView: UIStackView = { return UIStackView().with { $0.translatesAutoresizingMaskIntoConstraints = false @@ -64,10 +65,6 @@ open class TextArea: EntryFieldBase { $0.setContentCompressionResistancePriority(.required, for: .vertical) $0.textStyle = .bodySmall } - open var maxLengthLabel = Label().with { - $0.setContentCompressionResistancePriority(.required, for: .vertical) - $0.textStyle = .bodySmall - } //-------------------------------------------------- // MARK: - Public Properties @@ -96,9 +93,6 @@ open class TextArea: EntryFieldBase { super.setup() minWidthConstraint = containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: containerSize.width) minWidthConstraint?.isActive = true - characterCountLabel.text = "0" - maxLengthLabel.text = "/200" - controlContainerView.addSubview(textView) textView .pinTop() @@ -118,6 +112,8 @@ open class TextArea: EntryFieldBase { open override func reset() { super.reset() textView.text = "" + characterCountLabel.reset() + characterCountLabel.textStyle = .bodySmall } /// Container for the area in which the user interacts. @@ -132,7 +128,6 @@ open class TextArea: EntryFieldBase { textView.isEditable = isEnabled textView.textColor = textViewTextColorConfiguration.getColor(self) - //set the width constraints if let width { widthConstraint?.constant = width @@ -143,6 +138,12 @@ open class TextArea: EntryFieldBase { widthConstraint?.isActive = false 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. @@ -151,9 +152,18 @@ open class TextArea: EntryFieldBase { bottomStackView.pinToSuperView() bottomStackView.addArrangedSubview(bottomContainerView) bottomStackView.addArrangedSubview(characterCountLabel) - bottomStackView.addArrangedSubview(maxLengthLabel) 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 { @@ -177,9 +187,12 @@ extension TextArea: UITextViewDelegate { textViewHeightConstraint.isActive = true } - //setting the value and firing control event - value = textView.text - sendActions(for: .valueChanged) - + if textView.text.count <= allowCharCount { + //setting the value and firing control event + value = textView.text + sendActions(for: .valueChanged) + } else { + textView.text.removeLast() + } } }