简体   繁体   中英

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!

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

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM