0

Cell contains multiline text so I decided to use text view.

textView.textContainerInset = UIEdgeInsets.zero removes extra padding. Code to center it vertically:

extension UITextView {
    func centerVertically() {
        var topCorrect = (self.bounds.size.height - self.contentSize.height * self.zoomScale) / 2
        topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect;
        self.contentInset.top = topCorrect
    }
}

This works if I predefine cell height in tableView(_:, heightForRowAt:).

But if I use UITableView.automaticDimension to resize the cell everything becomes broken.

How to solve this issue?

Vyachaslav Gerchicov
  • 2,317
  • 3
  • 23
  • 49
  • You definitely shouldn't extend `UITextView` for this. This is not a good use case for extensions. You should also be using auto layout for basically all of your positioning, especially for self-sizing cells (how else are they supposed to self size, right?). I would remove the extension and implement auto layout in your cell and all of your problems will disappear. – trndjc Dec 10 '18 at 07:28
  • Don't use `UITextView` only for having multiline. Use `UILabel` instead. – nayem Dec 10 '18 at 09:14

1 Answers1

1
  1. Use auto layout and pin leading, trailing, top and bottom, constraints of textview to the cell.
  2. Disable scrolling in textview
  3. Add a height constraint such that height is greater than or equal to a constant value (this is a min value that will show on the screen) for textview
  4. User .automatic dimension

Try this and let me know if this worked out for you

Ashish
  • 204
  • 1
  • 8