Luc*_*ene 7 uitableview uilabel ios autolayout swift
我有一个UITableView与包含UILabel的单元格.UILabel有一个自定义的UIEdgeInset.我将UILabel子类化并设置UIEdgeInsets如下:
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
override var intrinsicContentSize: CGSize {
var contentSize = super.intrinsicContentSize
contentSize.width += leftInset + rightInset
contentSize.height += topInset + bottomInset
return contentSize
}
Run Code Online (Sandbox Code Playgroud)
但是当我在UILabel中有更多行时,标签有时会被截断.我已经将行高配置为UITableViewAutomaticDimension并设置了estimatedRowHeight.约束也很好.问题似乎是我正在设置UIEdgeInsets,因为如果我不自定义它可以正常工作.
可能我应该告诉单元格在设置insets后更新约束,但到目前为止我无法做到这一点.
故事板中添加的约束.Bottom,Top,Leading并且Trailing都与上海华盈(UITableViewCell中).所有常量都设置为0.
在cellForRowAtIndexPath代码中如下:
let cell = tableView.dequeueReusableCell(withIdentifier: "AnswersCell", for: indexPath) as! AnswerCell
cell.answerLabel.text = alternatives[indexPath.row]
return cell
Run Code Online (Sandbox Code Playgroud)
小智 -2
我像这样对标签进行了子类化:
class PaddingLabel: UILabel {
var topInset: CGFloat = 0
var bottomInset: CGFloat = 0
var leftInset: CGFloat = 0
var rightInset: CGFloat = 0
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
override var intrinsicContentSize: CGSize {
get {
var contentSize = super.intrinsicContentSize
contentSize.height += topInset + bottomInset
contentSize.width += leftInset + rightInset
return contentSize
}
}
}
Run Code Online (Sandbox Code Playgroud)
也许这就是get所缺少的。
| 归档时间: |
|
| 查看次数: |
1023 次 |
| 最近记录: |