Francisco Romero
Francisco Romero

Reputation: 13199

How can I set a multiline UILabel inside UITableCell on Swift 3?

I am trying to set a multiline UILabel, inside an UITableCell.

That UILabel has a proportional width taking as reference the screen of the device (with a multiplier). The text inside of it can change so I need to fit on the UILabel being able to multiline when the width of the text is higher than the space of the UILabel width.

I tried using:

myLabel.lineBreakMode = .byWordWrapping
myLabel.numberOfLines = 0
myLabel.sizeToFit()

but it is always displayed on one line truncating the text that overflows the UILabel width.

How can I make my UILabel to be multiline?

EDIT: I cannot put breaklines to my text because I retrieve my texts from my database.

Thanks in advance!

Upvotes: 0

Views: 1097

Answers (3)

Bhavuk Jain
Bhavuk Jain

Reputation: 2187

These two lines are necessary for calculating the cell height automatically. Put these in viewDidLoad.

self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension

Upvotes: 1

Francisco Romero
Francisco Romero

Reputation: 13199

Finally I was able to make my multiline UILabel adding this code:

self.tableView.estimatedRowHeight = 50.0

and reducing the multiplier value. It seems that the text could not fit on the height of the UITableCell.

Upvotes: 0

Sethmr
Sethmr

Reputation: 3084

If you want a label to switch from one line to multiple lines, you either have to specify line breaks in the text of the string with \n or newline char, or you can set constraints for the width of the label, or you can set leading and trailing constraints for the label. If one of those methods is complete, the label will go to as many lines as it takes to hit the max number of lines or infinite if set to 0.

Upvotes: 0

Related Questions