简体   繁体   English

如何在Swift 3的UITableCell内设置多行UILabel?

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

I am trying to set a multiline UILabel , inside an UITableCell . 我正在尝试在UITableCell内设置多行UILabel

That UILabel has a proportional width taking as reference the screen of the device (with a multiplier). UILabel具有以设备屏幕(带倍增器)为参考的比例width 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. 它里面的文本可以改变,所以我需要适应的UILabel能多当文本的宽度比空间更高UILabel的宽度。

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. 但是它总是显示在一行上,以截断溢出UILabel宽度的文本。

How can I make my UILabel to be multiline? 如何使UILabel成为多行?

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. 将它们放在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. 如果希望标签从一行切换到多行,则必须使用\\ n或换行符char在字符串的文本中指定换行符,或者可以设置标签宽度的约束,也可以设置标签的前导约束和尾随约束。 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. 如果这些方法之一完成,则标签将达到达到最大行数所需要的行数,如果设置为0,则行将变为无限。

Finally I was able to make my multiline UILabel adding this code: 最终,我能够使多行UILabel添加以下代码:

self.tableView.estimatedRowHeight = 50.0

and reducing the multiplier value. 并减小multiplier数值。 It seems that the text could not fit on the height of the UITableCell . 似乎该文本不能适合UITableCell的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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