关于对齐:
您可以创建与左标签相同高度的右标签。 然后尝试将文本居中对齐。为此,您可以子类化UILabel类并将文本矩形对齐方式更改为中间。
For Dynamic Height:
1. Use auto layout when creating your table view cells.
2. Set the table view rowHeight to equal UITableViewAutomaticDimension.
3. Set the estimatedRowHeight or implement the height estimation delegate method.
Pro Tip: the trick to getting auto layout to work on a UITableViewCell is to ensure you have constraints to pin each subview on all sides — that is, each subview should have leading, top, trailing and bottom constraints.
Furthermore, you need a clear line of constraints going from the top to the bottom of the contentView. This ensures that auto layout correctly determines the height of the contentView based on its subviews.
The tricky part is that Interface Builder often won’t warn you if you’re missing some of these constraints; auto layout simply doesn’t return the correct heights when you run the project. For example, it may return 0 for the cell height, which is a clue that your constraints need more work.
If you run into issues when working with your own projects, try adjusting your constraints until the above criteria are met.
For Detail, please refer to original source of above text is https://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift
tableView.estimatedRowHeight = 160
tableView.rowHeight = UITableViewAutomaticDimension
然后,在绘制xib单元格时,您可以遵循此模式:
+-view----------------+
| | |
| -label- |
| | |
| +-subview--------+ |
| | | | | |
|-| -label-label- |--|
| | | | | |
| | -label- | |
| | | | |
| +----------------+ |
| | |
| -label- |
| | |
+---------------------+
每个标签都设置了顶部、底部、前导和后续约束。 如果您需要压缩标签组,可以添加子视图将它们包含在内。