【问题标题】:Padding in UILabel iOS Swift [duplicate]UILabel iOS Swift中的填充[重复]
【发布时间】:2016-12-26 18:53:29
【问题描述】:

我已经从情节提要创建了一个标签,现在我正在尝试为该标签提供填充。我创建了一个类并尝试了以下两种方法,但没有任何效果。请帮忙。

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

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    试试这个,我已经用过,它从 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

    【讨论】:

    • 我在 tableViewCell 中有一个标签,我该如何应用它?帮助。谢谢。
    • 在uitableview的cellForRowat方法中实现相同的..到单元格的标签
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多