From 4f41d9cc93b07d6cf3481bbe8ae141b2827184ff Mon Sep 17 00:00:00 2001 From: vasavk Date: Mon, 26 Feb 2024 22:46:33 +0530 Subject: [PATCH] Digital ACT191 story ONEAPP-6682 added preset default height property. --- .../TextFields/TextArea/TextArea.swift | 54 +++++++++++++++---- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/VDS/Components/TextFields/TextArea/TextArea.swift b/VDS/Components/TextFields/TextArea/TextArea.swift index 135a2421..a9c1cbf4 100644 --- a/VDS/Components/TextFields/TextArea/TextArea.swift +++ b/VDS/Components/TextFields/TextArea/TextArea.swift @@ -69,11 +69,44 @@ open class TextArea: EntryFieldBase { $0.numberOfLines = 1 } + private var _minHeight: Height = .twoX + + open var minHeight: Height? { + get { return _minHeight } + set { + if let newValue { + _minHeight = newValue + } else { + _minHeight = .twoX + } + textViewHeightConstraint?.constant = _minHeight.value + setNeedsUpdate() + } + } + //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- override var containerSize: CGSize { CGSize(width: 182, height: 88) } - + + /// Enum used to describe the the height of TextArea. + public enum Height: String, CaseIterable { + case twoX = "2X" + case fourX = "4X" + case eightX = "8X" + + var value: CGFloat { + switch self { + case .twoX: + 88 + case .fourX: + 176 + case .eightX: + 352 + } + } + } + /// UITextView shown in the TextArea. open var textView = UITextView().with { $0.translatesAutoresizingMaskIntoConstraints = false @@ -139,6 +172,8 @@ open class TextArea: EntryFieldBase { textView.text = "" characterCounterLabel.reset() characterCounterLabel.textStyle = .bodySmall + minHeight = .twoX + setNeedsUpdate() } /// Container for the area in which the user interacts. @@ -230,7 +265,7 @@ open class TextArea: EntryFieldBase { let text = textView.attributedText?.mutableCopy() as? NSMutableAttributedString else { return } text.addAttribute(NSAttributedString.Key.backgroundColor, value: highlightBackgroundColor.getColor(self), range: NSRange(location:maxLength, length: (count - maxLength))) text.addAttribute(NSAttributedString.Key.foregroundColor, value: highlightTextColor.getColor(self), range: NSRange(location:maxLength, length: (count - maxLength))) - textView.attributedText = text.copy() as? NSAttributedString + textView.attributedText = text } } @@ -244,15 +279,15 @@ extension TextArea: UITextViewDelegate { //if you want it to work "as-is" delete this code //since it will autogrow with the current settings if let textViewHeightConstraint, textView.isEditable { - let height = textView.contentSize.height - if height > 88 && height < 176 { - textViewHeightConstraint.constant = 176 - } else if height > 176 { - textViewHeightConstraint.constant = 352 + var height = textView.contentSize.height + height = max(height, _minHeight.value) + if height > Height.twoX.value && height < Height.fourX.value { + textViewHeightConstraint.constant = Height.fourX.value + } else if height > Height.fourX.value { + textViewHeightConstraint.constant = Height.eightX.value } else { - textViewHeightConstraint.constant = 88 + textViewHeightConstraint.constant = Height.twoX.value } - textViewHeightConstraint.isActive = true } //The exceeding characters will be highlighted to help users correct their entry. @@ -261,7 +296,6 @@ extension TextArea: UITextViewDelegate { //setting the value and firing control event value = textView.text sendActions(for: .valueChanged) - if (textView.text.count > (maxLength ?? 0)) { highlightCharacterOverflow() }