Esera
Esera

Reputation: 277

Add a UITextView in a UITableViewCell with dynamic height with maximum height

I have a UITableViewCell, which has a UITextView inside. The UITextView is added in Storyboard and all necessary Constraints are set. The height of the UITextView should response to the content inside, but only to a maximum height, then the UITextView should stop growing and be scrollable instead. I have archieved that with the code below. My problem is, if I remove rows from my UITextView again, the UITextView won't shrink, or it shrinks, so the height is too small. What should I do?

/// Check if height of UITextView is bigger than maximum
if Int(textView.frame.size.height) >= 75 {
    textView.isScrollEnabled = true
    textView.frame.size.height = 74
}
else {
    textView.isScrollEnabled = false

    /// Change the UITableViewCells height if the UITextView did change
    let currentOffset = controller.tableView.contentOffset

    UIView.setAnimationsEnabled(false)

    controller.tableView.beginUpdates()
    controller.tableView.endUpdates()

    UIView.setAnimationsEnabled(true)

    controller.tableView.setContentOffset(currentOffset, animated: false)
}

Upvotes: 2

Views: 713

Answers (1)

Don
Don

Reputation: 529

I decided to give this a shot and it's working well. It's for a UITableView that only returns 1 row, so you'll need to do a little more work to keep track of the cells in a real app.

This is the UITableViewCell class:

import UIKit
class Cell: UITableViewCell {
    @IBOutlet weak var textView: UITextView!
}

Cell.xib Content View only contains the UITextView. I set the row height to 44 and then Constraints of TextView.top = top, bottom = TextView.bottom, trailing = TextView.trailing + 36, TextView.leading = leading + 36. The 36 for the leading and trailing constraints isn't important, I just wanted some space on the sides.

Here's the entire ViewController:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {

  @IBOutlet weak var tableView: UITableView!
  private var textViewHeight: CGFloat = 0.0

  override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.rowHeight = 44
    tableView.tableFooterView = UIView()
    tableView.register(UINib(nibName: "Cell", bundle: nil), forCellReuseIdentifier: "cell")
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
    cell.textView.delegate = self
    cell.textView.text = ""
    cell.textView.layer.borderColor = UIColor.lightGray.cgColor
    cell.textView.layer.borderWidth = 1
    cell.textView.layer.cornerRadius = 4
    return cell
  }

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return min(75, max(textViewHeight, 44))
  }

  func textViewDidChange(_ textView: UITextView) {
    let size = textView.bounds.size
    let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude))
    if size.height != newSize.height {
      textViewHeight = newSize.height
      UIView.setAnimationsEnabled(false)
      tableView.beginUpdates()
      tableView.endUpdates()
      UIView.setAnimationsEnabled(true)
    }
  }

}

Upvotes: 2

Related Questions