我有一份电话费率清单,textLabel
是国家/地区并且detailTextLabel
是我必须显示的速率。
对于某些字符串,textLabel
太长并且detailTextLabel
变得隐藏。是否有一个设置可以自动调整文本…
如果太长了?
这是一个例子,其中Central African Republic (Mobile)
有这个问题:
在使用 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
}
}
}
}
请注意,虽然detail.textAlignment = .Left
我们考虑了细节的宽度,实际文本最终右对齐。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)