我有一个电话费率列表,textLabel是国家/地区,detailTextLabel是我必须显示的费率.
对于某些字符串,textLabel它太长而且detailTextLabel变得隐藏.是否有设置自动调整文本,…如果它太长?
这是一个Central African Republic (Mobile)有这个问题的例子:

小智 5
在使用 UITableViewCellStyle.Value1 样式布置单元格时,标题标签似乎获得优先权并将详细信息标签推出视图。解决方案可能是继承 UITableViewCell 并覆盖其 layoutSubviews():
override func layoutSubviews() {
super.layoutSubviews()
if let detail = self.detailTextLabel {
// this will do the actual layout of the detail
// label's text, so you can get its width
detail.sizeToFit()
// you might want to find a clever way to calculate this
// instead of assigning a literal
let rightMargin: CGFloat = 16
// adjust the detail's frame
let detailWidth = rightMargin + detail.frame.size.width
detail.frame.origin.x = self.frame.size.width - detailWidth
detail.frame.size.width = detailWidth
detail.textAlignment = .Left
// now truncate the title label
if let text = self.textLabel {
if text.frame.origin.x + text.frame.size.width > self.frame.width - detailWidth {
text.frame.size.width = self.frame.width - detailWidth - text.frame.origin.x
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,尽管detail.textAlignment = .Left我们考虑了细节的宽度,但实际文本最终会向右对齐。
因此,您可能需要做的是手动修复 textLabel 的宽度,因为默认情况下它占据单元格的整个宽度。为此,您可以执行以下操作:
CGRect textLabelFrame = cell.textLabel.frame;
textLabelFrame.size.width -= DETAIL_LABEL_WIDTH;
cell.textLabel.frame = textLabelFrame;
Run Code Online (Sandbox Code Playgroud)
在 cellForRowAtIndexPath 中,其中 DETAIL_LABEL_WIDTH 是您想要的detailTextLabel 的宽度。假设标签是自动省略的(它应该是这样),如果宽度比您在上面设置的宽度长,这将导致文本在详细文本标签之前的标签末尾添加“...” 。
| 归档时间: |
|
| 查看次数: |
1731 次 |
| 最近记录: |