UITableView estimatedRowheight Dynamic Type

600 views Asked by At

I'm using self-sizing table view cells to support Dynamic Type.

When the user sets their font size to normal, the row height is 44. But, when the user adjusts their font to be smaller or larger, the row height decreases or increases, respectively.

So, what should I set the estimatedRowHeight to, since rowHeight changes depending on the user's Dynamic Type settings?

2

There are 2 answers

0
Fraser On

When the default Dynamic Type is chosen the .body pointSize is 17.0, so I use this calculation

self.tableView.estimatedRowHeight = [yourEstimateForDefaultSize] / 17.0 * UIFont.preferredFont(forTextStyle: .body).pointSize

For the default Dynamic Type size this estimate is accurate. For other sizes it loses accuracy, but its an improvement

0
Milan Nosáľ On

I would probably try to estimate the height dynamically using a delegate method:

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
    return topOffset + bottomOffset + fontSize * countOfLines
}