【发布时间】:2017-11-07 09:46:16
【问题描述】:
我已经使用 StackOverflow 的 popular thread 设置了我的 UILabel 填充以解决自动布局问题。 这个线程基本上是一个 UILabel 扩展。
部分答案是:
class NRLabel : UILabel {
var textInsets = UIEdgeInsets.zero {
didSet { invalidateIntrinsicContentSize() }
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let insetRect = UIEdgeInsetsInsetRect(bounds, textInsets)
let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
let invertedInsets = UIEdgeInsets(top: -textInsets.top,
left: -textInsets.left,
bottom: -textInsets.bottom,
right: -textInsets.right)
return UIEdgeInsetsInsetRect(textRect, invertedInsets)
}
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, textInsets))
}
}
一切正常,已添加填充,但我需要重新加载 tableViewcell 才能看到效果。
我已经像这样为 padding
覆盖了我的 customCell 的 viewWillLayoutSubviews() 函数 self.chatLabel.textInsets = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
而且效果是这样的。
你看,第一个标签在重新加载单元格后得到了填充。
请建议如何使用上述扩展实现UILabel填充,以便解决此问题。
【问题讨论】:
-
不清楚您要达到的目标。你想要一个带填充的 UILabel 吗?有什么理由不将标签作为 UIView 的子视图,而只使用自动布局和约束?
-
@elk_cloner 你在你的单元格中使用了 cell.dequeue 吗?
-
我使用了自定义单元格。我想要带有填充的 ui 标签
-
我认为问题不在于标签...请检查表格单元格高度...是否相同?...请检查。
标签: ios iphone swift uitableview uilabel