From 13f2cc9d2906d196d336077a3a7abec73df96292 Mon Sep 17 00:00:00 2001 From: "Evan W. Patton" Date: Mon, 9 Dec 2024 15:36:47 -0500 Subject: [PATCH] Make empty state a computable property for better multiline support Change-Id: I9ed27078bae492de15aa7c77848138fef4d36008 --- appinventor/components-ios/src/TextBox.swift | 30 +++++++++++--------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/appinventor/components-ios/src/TextBox.swift b/appinventor/components-ios/src/TextBox.swift index c339d7d5d6b..727f497ec19 100644 --- a/appinventor/components-ios/src/TextBox.swift +++ b/appinventor/components-ios/src/TextBox.swift @@ -17,7 +17,6 @@ class TextBoxAdapter: NSObject, TextBoxDelegate { private var _numbersOnly = false private var _multiLine = false - private var _empty = true private var _readOnly = false private weak var _base: TextBoxBase? = nil private var _placeholderColor: Int32 = Color.default.int32 @@ -54,6 +53,10 @@ class TextBoxAdapter: NSObject, TextBoxDelegate { _field.inputAccessoryView = getAccesoryView(selector) } + var isEmpty: Bool { + _field.text?.isEmpty ?? true + } + open var view: UIView { get { return _wrapper @@ -86,7 +89,7 @@ class TextBoxAdapter: NSObject, TextBoxDelegate { } set(color) { _field.textColor = color - _view.textColor = _empty ? kDefaultPlaceholderColor : color + _view.textColor = isEmpty ? kDefaultPlaceholderColor : color } } @@ -111,11 +114,11 @@ class TextBoxAdapter: NSObject, TextBoxDelegate { } open func updatePlaceholder() { - var newPlaceholder = NSAttributedString(string: placeholderText ?? "", + let newPlaceholder = NSAttributedString(string: placeholderText ?? "", attributes: [NSAttributedString.Key.foregroundColor:argbToColor(_placeholderColor), - NSAttributedString.Key.font: _field.font]) + NSAttributedString.Key.font: _field.font as Any]) _field.attributedPlaceholder = newPlaceholder - if _empty { + if isEmpty { _view.attributedText = newPlaceholder } } @@ -126,7 +129,7 @@ class TextBoxAdapter: NSObject, TextBoxDelegate { } set(text) { _field.placeholder = text - if _empty { + if isEmpty { _view.text = text } updatePlaceholder() @@ -147,9 +150,11 @@ class TextBoxAdapter: NSObject, TextBoxDelegate { get { return _multiLine ? _view.text: _field.text } - set(text) { + set { + let text = newValue ?? "" _field.text = text - _view.text = text + _view.text = isEmpty ? _field.placeholder : text + _view.textColor = isEmpty ? kDefaultPlaceholderColor : _field.textColor } } @@ -186,9 +191,8 @@ class TextBoxAdapter: NSObject, TextBoxDelegate { } fileprivate func setEmpty(_ shouldEmpty: Bool) { - _empty = shouldEmpty - _view.text = _empty ? _field.placeholder: nil - _view.textColor = _empty ? kDefaultPlaceholderColor : _field.textColor + _view.text = shouldEmpty ? _field.placeholder : nil + _view.textColor = shouldEmpty ? kDefaultPlaceholderColor : _field.textColor } private func makeMultiLine() { @@ -212,7 +216,7 @@ class TextBoxAdapter: NSObject, TextBoxDelegate { } func textViewDidBeginEditing(_ textView: UITextView) { - if _empty { + if isEmpty { setEmpty(false) } } @@ -226,7 +230,7 @@ class TextBoxAdapter: NSObject, TextBoxDelegate { } func textFieldDidBeginEditing(_ textField: UITextField) { - if _empty { + if isEmpty { setEmpty(false) } _base?.GotFocus()