在 UITableViewCell 中动态调整 UILabel? [英] Adjust UILabel Dynamically Within UITableViewCell?

查看:37
本文介绍了在 UITableViewCell 中动态调整 UILabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态调整 UILabel 中的行数.例如,可能有一个长字符串或一个短字符串.如果少于 10 个字符,则应为一行.如果有10-20个字符,应该是两行.此 UILabel 位于 UITableViewCell 内.目前,如果有一个长字符串,那么 UILabel 只会在空间不足的地方显示...",因此该字符串被切断.我怎样才能防止这种情况发生?

I would like to adjust the number of lines in a UILabel dynamically. For example, there could be a long string or a short string. If there are less than 10 characters, there should be one line. If there are 10-20 characters, it should be two lines. This UILabel is inside of a UITableViewCell. Currently, if there is a long string, then the UILabel just shows "..." wherever it runs out of space, so the string is cut off. How can I prevent this from happening?

推荐答案

你需要给tableview单元格的标签leader, trailing, top and bottom 做如下约束...

You need to give constraints to tableview cell's label leading, trailing, top and bottom like below...

还将标签的行数设置为零,将换行模式设置为自动换行.

Also set label's number of lines to zero and line breaking mode to word wrap.

在你的主视图控制器中添加这一行...

And in your main view controller just add this line...

self.tableView.estimatedRowHeight = UITableViewAutomaticDimension

以上行根据其内容自动计算单元格高度.

The above line automatically calculates the cell height as per its content.

这篇关于在 UITableViewCell 中动态调整 UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆