Skip to content

Commit

Permalink
Make empty state a computable property for better multiline support
Browse files Browse the repository at this point in the history
Change-Id: I9ed27078bae492de15aa7c77848138fef4d36008
  • Loading branch information
ewpatton authored and SusanRatiLane committed Dec 11, 2024
1 parent d771369 commit 13f2cc9
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions appinventor/components-ios/src/TextBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -86,7 +89,7 @@ class TextBoxAdapter: NSObject, TextBoxDelegate {
}
set(color) {
_field.textColor = color
_view.textColor = _empty ? kDefaultPlaceholderColor : color
_view.textColor = isEmpty ? kDefaultPlaceholderColor : color
}
}

Expand All @@ -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
}
}
Expand All @@ -126,7 +129,7 @@ class TextBoxAdapter: NSObject, TextBoxDelegate {
}
set(text) {
_field.placeholder = text
if _empty {
if isEmpty {
_view.text = text
}
updatePlaceholder()
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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() {
Expand All @@ -212,7 +216,7 @@ class TextBoxAdapter: NSObject, TextBoxDelegate {
}

func textViewDidBeginEditing(_ textView: UITextView) {
if _empty {
if isEmpty {
setEmpty(false)
}
}
Expand All @@ -226,7 +230,7 @@ class TextBoxAdapter: NSObject, TextBoxDelegate {
}

func textFieldDidBeginEditing(_ textField: UITextField) {
if _empty {
if isEmpty {
setEmpty(false)
}
_base?.GotFocus()
Expand Down

0 comments on commit 13f2cc9

Please sign in to comment.