[英]Change uilabel height based on content in a tableview cell
I want to expand label's height based on the text inside. 我想根据里面的文字扩展标签的高度。 I use tableviewcontroller with static cells.
我使用tableviewcontroller与静态单元格。
I have an UILabel
in a tableview cell and I set top, bottom, right and left constraints of the label. 我在tableview单元格中有一个
UILabel
,我设置了标签的顶部,底部,右侧和左侧约束。
I tried these solutions: 我试过这些解决方案:
self.eDesc.numberOfLines = 0
self.eDesc.lineBreakMode = NSLineBreakMode.byWordWrapping
self.eDesc.sizeToFit()
self.tableView.estimatedRowHeight = 140
self.tableView.rowHeight = UITableViewAutomaticDimension
But none of them worked. 但它们都没有奏效。
Is there any other approach on this (maybe changing size by label frame)? 有没有其他方法(可能通过标签框架改变大小)?
To set automatic dimension for row height & estimated row height, ensure following steps to make, auto dimension effective for cell/row height layout. 要设置行高和估计行高的自动尺寸,请确保执行以下步骤,自动尺寸对单元格/行高度布局有效。 I just tested following steps and code and works fine.
我刚刚测试了以下步骤和代码并且运行正常。
UITableViewAutomaticDimension
to rowHeight & estimatedRowHeight UITableViewAutomaticDimension
分配给rowHeight和estimatedRowHeight heightForRowAt
and return a value UITableViewAutomaticDimension
to it) heightForRowAt
并向其返回值UITableViewAutomaticDimension
) - -
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Don't forget to set dataSource and delegate for table
table.dataSource = self
table.delegate = self
// Set automatic dimensions for row height
// Swift 4.2 onwards
table.rowHeight = UITableView.automaticDimension
table.estimatedRowHeight = UITableView.automaticDimension
// Swift 4.1 and below
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
}
// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// Swift 4.2 onwards
return UITableView.automaticDimension
// Swift 4.1 and below
return UITableViewAutomaticDimension
}
For label instance in UITableviewCell 对于UITableviewCell中的标签实例
Edit : You must be missing top or bottom or height layout constraint for anyone UIElement in cell. 编辑 :您必须缺少单元格中任何UIElement的顶部或底部或高度布局约束。 Ensure all UIElements in cell, have valid top and bottom (and height, whereever required) constraints attached with it vertical (adjacent) UIElement.
确保单元格中的所有UIElements具有与其垂直(相邻)UIElement附加的有效顶部和底部(以及高度,必要时)约束。
Note : If you've more than one labels (UIElements) with dynamic length, which should be adjusted according to its content size: Adjust 'Content Hugging and Compression Resistance Priority` for labels which you want to expand/compress with higher priority. 注意 :如果您有多个具有动态长度的标签(UIElements),应根据其内容大小进行调整:针对要以更高优先级展开/压缩的标签调整“内容拥抱和压缩阻力优先级”。
Here in this example I set low hugging and high compression resistance priority, that leads to set more priority/importance for contents of second (yellow) label. 在这个例子中,我设置了低拥抱和高压缩阻力优先级,这导致为第二(黄色)标签的内容设置更多优先级/重要性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.