Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap Text in UITableView Custom Cell When It Reaches End of Screen (Swift)

I have a table that loads data from a database but the problem is if the text being loaded is too long, it goes off the screen. I'm trying to find a way for the text to go onto the next line and have the cell resize automatically to fit this change. Does anyone know how this is done? The cell has three labels but one of them is allowed to be multi-lined.

EDIT:

I got it to work using auto constraints but how can I resize the table cell so that the actual items fit inside the cell and do not go over the cell boundary?

like image 806
Navio53 Avatar asked May 16 '15 02:05

Navio53


2 Answers

Set label number of "Lines" to ZERO. And set "Line Breaks" to "Word Wrap"

like image 118
Yu's Way Avatar answered Nov 16 '22 23:11

Yu's Way


Adding these two methods along with above code fixed the problem with custom font

tamilRow.textLabel?.numberOfLines=0
tamilRow.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension }

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
like image 6
Joseph Selvaraj Avatar answered Nov 16 '22 23:11

Joseph Selvaraj