Просмотр исходного кода

TextFieldCell: set placeholder only if textfield is empty, otherwise the textfield might occupy more space than actually needed

cyberta 5 лет назад
Родитель
Сommit
387cdf4b0c
1 измененных файлов с 15 добавлено и 2 удалено
  1. 15 2
      deltachat-ios/View/TextFieldCell.swift

+ 15 - 2
deltachat-ios/View/TextFieldCell.swift

@@ -3,12 +3,15 @@ import UIKit
 class TextFieldCell: UITableViewCell {
 
     private let maxFontSizeHorizontalLayout: CGFloat = 30
+
+    var placeholderVal: String
     var placeholder: String? {
         set {
-            textField.placeholder = newValue
+            placeholderVal = newValue ?? ""
+            configureTextFieldPlaceholder()
         }
         get {
-            return textField.placeholder
+            return placeholderVal
         }
     }
 
@@ -66,6 +69,7 @@ class TextFieldCell: UITableViewCell {
     
     init(description: String, placeholder: String, delegate: UITextFieldDelegate? = nil) {
         preferValueOnWrite = true
+        placeholderVal = placeholder
         super.init(style: .default, reuseIdentifier: nil)
         title.text = "\(description):"
 
@@ -105,9 +109,18 @@ class TextFieldCell: UITableViewCell {
 
     @objc func textFieldChanged() {
         preferValue = preferValueOnWrite
+        configureTextFieldPlaceholder()
         onTextFieldChange?(self.textField)
     }
 
+    func configureTextFieldPlaceholder() {
+        if let textFieldText = textField.text, !textFieldText.isEmpty {
+            textField.placeholder = nil
+        } else {
+            textField.placeholder = placeholderVal
+        }
+    }
+
     func getText() -> String? {
         if let text = textField.text {
             if text.isEmpty {