2016-08-19 72 views
0

我從storyboard創建了一個標籤,現在我試圖給該標籤填充內容。我創建了一個類並嘗試了下面的兩種方法,但沒有任何工作。請幫忙。UILabel iOS中的填充Swift

override func drawTextInRect(rect: CGRect) { 
     super.drawTextInRect(UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0))) 

    } 


let padding = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10) 


override func intrinsicContentSize() -> CGSize { 
    let superContentSize = super.intrinsicContentSize() 
    let width = superContentSize.width + padding.left + padding.right 
    let heigth = superContentSize.height + padding.top + padding.bottom 
    return CGSize(width: width, height: heigth) 
} 

回答

4

試試這個,我已經使用並從工作這麼本身

@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 drawTextInRect(rect: CGRect) { 
     let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset) 
     super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets)) 
    } 

    override func intrinsicContentSize() -> CGSize { 
     var intrinsicSuperViewContentSize = super.intrinsicContentSize() 
     intrinsicSuperViewContentSize.height += topInset + bottomInset 
     intrinsicSuperViewContentSize.width += leftInset + rightInset 
     return intrinsicSuperViewContentSize 
    } 
} 

參考:Adding space/padding to a UILabel

+0

我有一個tableViewCell標籤,如何申請呢? heeelp。謝謝 。 –

+0

在uitableview的cellForRowat方法中實現相同的單元格標籤 –