others-how to set multiline of tableviewcell and auto height of label in xcode


How to set auto height of a UILabel in xcode? How to set the multiline mode of the UILabel in xcode? Let’s do it.


  • MacOS 10.14
  • XCode 11.3
  • Swift 5


Step 1

You must set the constraints of the UILabel like following:


  • Top
  • Bottom
  • Leading
  • Trailing

Step 2

Set the numberOfLines properties for the UILabel:

import UIKit

class MessageTableViewCell: UITableViewCell {

    @IBOutlet weak var body: UILabel!
    override func awakeFromNib() {
        body.numberOfLines = 0


The numberOfLines=0 means unlimited lines of texts in the UILabel

Step 3

Set the automaticDimension for the tableview, you can do this in the UITableViewController like this:

class MessagesTableViewController: UITableViewController {
    override func viewDidLoad() {

        //auto ajust the tableview cell height
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 300

Ok, everything is done.