Search code examples
iosswiftxcodetableviewcell

How do I auto adjust the font size of a cells content to fit the tableview width?


I have a table view that shows the user what trips they have logged in. Except if the trip name is too long and doesn't fit within the table views width, it doesn't fully show. I would hope it's possible to simply resize the font so that it can fit within the table view, similar to the "Adjust to Fit" option that text fields have. Thanks!


Solution

  • As long as your label is properly constrained within the tableViewCell, you can simply set:

    label.adjustsFontSizeToFitWidth = true
    

    Or if you're using the default UITableViewCell's textLabel, then in cellForRowAtIndexPath:

    cell.textLabel?.adjustsFontSizeToFitWidth = true