Przeglądaj źródła

improve self-sizing of MultilineTextFieldCell

cyberta 5 lat temu
rodzic
commit
10ed6546fc

+ 1 - 8
deltachat-ios/Controller/EditSettingsController.swift

@@ -65,6 +65,7 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
         avatarSelectionCell.onAvatarTapped = { [weak self] in
             self?.onAvatarTapped()
         }
+        tableView.rowHeight = UITableView.automaticDimension
     }
 
     override func viewWillDisappear(_ animated: Bool) {
@@ -102,14 +103,6 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
         }
     }
 
-    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
-        if indexPath.section == section1 && indexPath.row == section1Status {
-            return MultilineTextFieldCell.cellHeight
-        } else {
-            return UITableView.automaticDimension
-        }
-    }
-
     override func tableView(_: UITableView, titleForFooterInSection section: Int) -> String? {
         if section == section1 {
             return String.localized("pref_who_can_see_profile_explain")

+ 15 - 5
deltachat-ios/View/MultilineTextFieldCell.swift

@@ -3,9 +3,12 @@ import UIKit
 
 class MultilineTextFieldCell: UITableViewCell, UITextViewDelegate {
 
-    private static let padding: CGFloat = 16
-    static var cellHeight: CGFloat {
-        return UIFont.preferredFont(forTextStyle: .body).pointSize * 6 + MultilineTextFieldCell.padding
+    private lazy var textFieldHeightConstraint: NSLayoutConstraint = {
+        return textField.constraintHeightTo(fourLinesHeight)
+    }()
+
+    private var fourLinesHeight: CGFloat {
+        return UIFont.preferredFont(forTextStyle: .body).pointSize * 4
     }
 
     var onTextFieldChange:((_:UITextView) -> Void)?    // set this from outside to get notified about textfield changes
@@ -64,9 +67,9 @@ class MultilineTextFieldCell: UITableViewCell, UITextViewDelegate {
 
         textField.alignLeadingToAnchor(margins.leadingAnchor, paddingLeading: -5)
         textField.alignTrailingToAnchor(margins.trailingAnchor)
-        let fontsize = UIFont.preferredFont(forTextStyle: .body).pointSize
-        contentView.addConstraint(textField.constraintHeightTo(fontsize * 4))
+        contentView.addConstraint(textFieldHeightConstraint)
         textField.alignTopToAnchor(descriptionField.bottomAnchor)
+        textField.alignBottomToAnchor(margins.bottomAnchor)
 
         placeholder.alignLeadingToAnchor(margins.leadingAnchor)
         placeholder.alignTrailingToAnchor(textField.layoutMarginsGuide.trailingAnchor)
@@ -97,4 +100,11 @@ class MultilineTextFieldCell: UITableViewCell, UITextViewDelegate {
         placeholder.isHidden = !(text.isEmpty && range.length == textView.text.count)
         return true
     }
+
+    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
+        if previousTraitCollection?.preferredContentSizeCategory !=
+            traitCollection.preferredContentSizeCategory {
+            textFieldHeightConstraint.constant = fourLinesHeight
+        }
+    }
 }