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에서 레이블이, 내가이 어떻게 적용 할 수 있는가? 고관절. 감사 . –

+0

는 uitableview의 cellForRowat 메서드에서 셀의 레이블에 동일한 것을 구현합니다. –