美文网首页
iOS中intrinsicContentSize的应用

iOS中intrinsicContentSize的应用

作者: 迷路的小小 | 来源:发表于2022-01-07 16:22 被阅读0次

    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()
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS中intrinsicContentSize的应用

          本文链接:https://www.haomeiwen.com/subject/nrijcrtx.html