2016-08-19 7 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

はこれを試してみてください、私が使用していると、それはSO自体

@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 
    } 
} 

リファレンスから働いていた:私は、これを適用する方法、tableViewCellにラベルを持ってAdding space/padding to a UILabel

+0

?ヒール。ありがとう。 –

+0

は、uitableviewのcellForRowatメソッドで同じものをセルのラベルに実装します –

関連する問題