2016-08-19 2 views
0

Я создал ярлык из раскадровки, и теперь я пытаюсь дать отступы для этого ярлыка. Я создал класс и пробовал оба метода ниже, но ничего не работает. Пожалуйста помоги.Прокладка в 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 
    } 
} 

Ссылка: Adding space/padding to a UILabel

+0

У меня есть ярлык в tableViewCell, как я могу применить это? heeelp. Благодарю . –

+0

реализовать то же самое в cellForRowat-методе uitableview .. на ярлыке ячейки –

Смежные вопросы