UILable在Autolayout模式下面自動調節字體大小

1、需求swift

  固定UILabel的寬度大小在必定範圍,內容可以自動伸縮ide

 

2、實施字體

  首先加好約束:ui

  

  約束加好以後,須要設置好Autoshrink屬性,包括Line break、BaseLine、以及縮小字體所可以達到的最小字體大小。spa

  

   若是須要在UILabel中添加padding,能夠增長如下代碼:blog

  

@IBDesignable class PaddingLabel: UILabel {

    @IBInspectable var topInset: CGFloat = 5.0
    @IBInspectable var bottomInset: CGFloat = 5.0
    @IBInspectable var leftInset: CGFloat = 7.0
    @IBInspectable var rightInset: CGFloat = 7.0

    override func drawText(in rect: CGRect) {
        let insets = UIEdgeInsets.init(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
        super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
    }

    override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(width: size.width + leftInset + rightInset,
                      height: size.height + topInset + bottomInset)
    }    
}

  https://stackoverflow.com/questions/27459746/adding-space-padding-to-a-uilabelit

相關文章
相關標籤/搜索