如何使UILabel在视图宽度更改时正确调整其尺寸?
我有一个UILabel配置为根据文本长度自动调整多行大小。标签处于动态调整大小的UITableViewCell中,对所有大小都有约束。如何使UILabel在视图宽度更改时正确调整其尺寸?
该代码将UILabel原点移动到右侧,从而缩小UILabel宽度。
该标签正确调整大小并包装文本。在将原点定位到其原始位置的过程中,UILabel的高度将扩展3行。但只需要两行文字。
高度扩展到3线的原因是,在返回原点回到原点与观看动画的过程中,文本扩展到3线,减少回2.
是否有电话或设置为使这将有正确的单元格大小?还是2有一种方法可以防止细胞在来源发生变化时调整大小。尤其是。随着宽度缩小和展开,是否有可能暂时禁用UILabel多行选项?
以下是显示行为的屏幕截图。 (1)标签在改变原点之前在UITableViewCell中的位置。
图(2)的UILabel原产向右移动,降低了标签的宽度。注意第二行有两行,标签边框紧紧地拥抱着文本。
图(3)来源返回第一图像的位置。请注意,第二行的UI标签松散地拥抱文本,并且不会返回到原始高度。
@IBAction func editTable(_ sender: UIBarButtonItem) {
if isEditingDynamic {
let animate = UIViewPropertyAnimator(duration: 0.4, curve: .easeIn) {
for cell in self.tableView.visibleCells as! [CellResize] {
cell.lblDescription.frame.origin.x = 16
}
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
animate.addCompletion(){
(position:UIViewAnimatingPosition) in
for cell in self.tableView.visibleCells as! [CellResize] {
cell.leadingContraint.constant = 0
}
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
animate.startAnimation()
isEditingDynamic = false
} else {
let animate = UIViewPropertyAnimator(duration: 0.4, curve: .easeIn) {
for cell in self.tableView.visibleCells as! [CellResize] {
cell.lblDescription.frame.origin.x = 60
}
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
animate.addCompletion(){
(position:UIViewAnimatingPosition) in
for cell in self.tableView.visibleCells as! [CellResize] {
cell.leadingContraint.constant = 54
}
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
animate.startAnimation()
isEditingDynamic = true
}
}
你能张贴你的表视图细胞和控制器是如何设置的一些代码?通常情况下,我会做什么是我将创建一个单行的UILabel并将其放置在单元格的内容视图中,用height constraint >= current
& numberOfLines = 0
其约束Top
,Left
,Trailing
& Leading
。
然后,在tableview cell
中,我将设置它的estimatedHeight to be 100
和heightForRow to be UITableViewAutomaticDimension
。
在这种情况下,TableView
将根据标签的内容自动处理单元格的高度。
是的,我已经配置了单元格,使其具有所有属性的动态特性,除了高度之外。由于四面都设置完毕,所以没有高度限制。问题在于调整大小的动画。我可以看到标签调整为原始尺寸时,UILabel将文本从2行换行为3行,然后在文本结束前回到2。一旦标签设置为3行,它不会将高度重新调整为2行,直到标签原点再次变小。 – Litehouse