Digital ACT191 story ONEAPP-6682 highlight text color, background color for character counter

This commit is contained in:
vasavk 2024-02-26 14:54:15 +05:30
parent f7134b9b8c
commit 5b01027554

View File

@ -87,6 +87,21 @@ open class TextArea: EntryFieldBase {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false) $0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false)
}.eraseToAnyColorable() { didSet { setNeedsUpdate() } } }.eraseToAnyColorable() { didSet { setNeedsUpdate() } }
/// Color configuration for error icon.
internal var iconColorConfiguration = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal)
}
/// Color configuration for character counter's highlight background color
internal var highlightBackgroundColor = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.backgroundPrimaryDark, VDSColor.backgroundPrimaryLight, forState: .normal)
}
/// Color configuration for character counter's highlight text color
internal var highlightTextColor = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOndark, VDSColor.elementsPrimaryOnlight, forState: .normal)
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Overrides // MARK: - Overrides
//-------------------------------------------------- //--------------------------------------------------
@ -106,7 +121,6 @@ open class TextArea: EntryFieldBase {
textViewHeightConstraint?.isActive = true textViewHeightConstraint?.isActive = true
backgroundColorConfiguration.setSurfaceColors(VDSColor.feedbackSuccessBackgroundOnlight, VDSColor.feedbackSuccessBackgroundOndark, forState: .success) backgroundColorConfiguration.setSurfaceColors(VDSColor.feedbackSuccessBackgroundOnlight, VDSColor.feedbackSuccessBackgroundOndark, forState: .success)
borderColorConfiguration.setSurfaceColors(VDSColor.feedbackSuccessOnlight, VDSColor.feedbackSuccessOndark, forState: .success) borderColorConfiguration.setSurfaceColors(VDSColor.feedbackSuccessOnlight, VDSColor.feedbackSuccessOndark, forState: .success)
textView.delegate = self textView.delegate = self
} }
@ -151,13 +165,15 @@ open class TextArea: EntryFieldBase {
} }
icon.size = .medium icon.size = .medium
icon.color = iconColorConfiguration.getColor(self)
containerView.layer.borderColor = readOnly ? readOnlyBorderColorConfiguration.getColor(self).cgColor : borderColorConfiguration.getColor(self).cgColor containerView.layer.borderColor = readOnly ? readOnlyBorderColorConfiguration.getColor(self).cgColor : borderColorConfiguration.getColor(self).cgColor
textView.isEditable = readOnly ? false : true textView.isEditable = readOnly ? false : true
textView.backgroundColor = backgroundColorConfiguration.getColor(self) textView.backgroundColor = backgroundColorConfiguration.getColor(self)
characterCounterLabel.textColorConfiguration = primaryColorConfiguration.eraseToAnyColorable() characterCounterLabel.textColorConfiguration = primaryColorConfiguration.eraseToAnyColorable()
textView.tintColor = iconColorConfiguration.getColor(self)
} }
/// Container for the area which shows helper text, error text, character count, max length value. /// Container for the area showing helper text, error text, character count, maximum length value.
open override func getBottomContainer() -> UIView { open override func getBottomContainer() -> UIView {
bottomView.addSubview(bottomStackView) bottomView.addSubview(bottomStackView)
bottomStackView.pinToSuperView() bottomStackView.pinToSuperView()
@ -182,6 +198,15 @@ open class TextArea: EntryFieldBase {
return ("\(countStr)" + "/" + "\(maxLength ?? 0)") return ("\(countStr)" + "/" + "\(maxLength ?? 0)")
} }
} }
open func highlightCharacterOverflow() {
let count = textView.text.count
print("count: \(count), maxLength: \(maxLength ?? 0)")
guard let text = textView.attributedText?.mutableCopy() as? NSMutableAttributedString else { return }
text.addAttribute(NSAttributedString.Key.backgroundColor, value: highlightBackgroundColor.getColor(self), range: NSRange(location:(maxLength ?? 0 ), length: (count - (maxLength ?? 0))))
text.addAttribute(NSAttributedString.Key.foregroundColor, value: highlightTextColor.getColor(self), range: NSRange(location:(maxLength ?? 0 ), length: (count - (maxLength ?? 0))))
textView.attributedText = text.copy() as? NSAttributedString
}
} }
extension TextArea: UITextViewDelegate { extension TextArea: UITextViewDelegate {
@ -189,29 +214,35 @@ extension TextArea: UITextViewDelegate {
// MARK: - UITextViewDelegate // MARK: - UITextViewDelegate
//-------------------------------------------------- //--------------------------------------------------
public func textViewDidChange(_ textView: UITextView) { public func textViewDidChange(_ textView: UITextView) {
//dynamic textView Height sizing based on Figma //dynamic textView Height sizing based on Figma
//if you want it to work "as-is" delete this code //if you want it to work "as-is" delete this code
//since it will autogrow with the current settings //since it will autogrow with the current settings
if let textViewHeightConstraint, textView.isEditable { if let textViewHeightConstraint, textView.isEditable {
let height = textView.contentSize.height let height = textView.contentSize.height
if height > 88 && height < 176 { if height > 88 && height < 176 {
textViewHeightConstraint.constant = 176 textViewHeightConstraint.constant = 176
} else if height > 176 { } else if height > 176 {
textViewHeightConstraint.constant = 352 textViewHeightConstraint.constant = 352
} else { } else {
textViewHeightConstraint.constant = 88 textViewHeightConstraint.constant = 88
} }
textViewHeightConstraint.isActive = true textViewHeightConstraint.isActive = true
} }
//The exceeding characters will be highlighted to help users correct their entry.
if ((maxLength ?? 0) > 0) { if ((maxLength ?? 0) > 0) {
if textView.text.count <= allowCharCount { if textView.text.count <= allowCharCount {
//setting the value and firing control event //setting the value and firing control event
value = textView.text value = textView.text
sendActions(for: .valueChanged) sendActions(for: .valueChanged)
if (textView.text.count > (maxLength ?? 0)) {
highlightCharacterOverflow()
}
} else { } else {
textView.text.removeLast() textView.text.removeLast()
highlightCharacterOverflow()
} }
} else { } else {
//setting the value and firing control event //setting the value and firing control event