1. 什么是intrinsicContentSize
Intrinsic Content Size:固有尺寸。
2. 带有边框的UILabel
public class PaddingLabel: UILabel {
public var padding: UIEdgeInsets = .zero
public override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: padding))
}
public override var intrinsicContentSize: CGSize {
var size = super.intrinsicContentSize
size.width += padding.left + padding.right
size.height += padding.top + padding.bottom
return size
}
}
3. 自动高度的UITableView
/// 自适应大小Table View
public class IntrinsicTableView: UITableView {
public var maxHeigth: CGFloat?
public override var intrinsicContentSize: CGSize {
if let maxHeigth = maxHeigth {
return CGSize(width: contentSize.width, height: min(contentSize.height, maxHeigth))
}
return contentSize
}
public override func layoutSubviews() {
super.layoutSubviews()
invalidateIntrinsicContentSize()
}
}
网友评论