UILabel border and padding

时间:2016-11-03 15:40:29

标签: ios swift

I want to add padding inside my label in order to have spaces between it and its border. I created for that a class that extends from UILabel.

UILabelPadding.swift:

import UIKit

class UILabelPadding: UILabel {

    let padding = UIEdgeInsets(top: 30, left: 30, bottom: 30, right: 30)
    override func drawText(in rect: CGRect) {
        super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
    }

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

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


}

and I changed the type of myLabel from UILabel to UILabelPadding. In my UIViewController, I set the text of myLabel, call the sizeToFit() and after that I add the border and the background color to myLabel:

   myLabel.text = "label test"
   myLabel.sizeToFit()
    //background + border
    myLabel.layer.borderColor  = UIColor(red: 27/255, green: 100/255, blue: 90/255,  alpha: 1.0).cgColor
    myLabel.layer.backgroundColor = UIColor(red: 27/255, green: 100/255, blue: 90/255,  alpha: 1.0).cgColor
    myLabel.layer.cornerRadius = 9
    myLabel.layer.masksToBounds = true
    myLabel.layer.borderWidth = 1

The border and the background color are added but the padding is not working. When I debug, the sizeThatFits() is never called.

Any help please?

4 个答案:

答案 0 :(得分:16)

我解决了这个问题!

对于那些面临同样问题的人:

1-从UILabel扩展课程:

UILabelPadding.swift:

class UILabelPadding: UILabel {

    let padding = UIEdgeInsets(top: 2, left: 8, bottom: 2, right: 8)
    override func drawText(in rect: CGRect) {
        super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
    }

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



}

2-将标签类型设置为UILabelPadding,并确保在故事板中也设置了类型。

答案 1 :(得分:2)

我使用了一个标签子类,它完成了你所描述的内容。它看起来像这样:

class MyBoundedLabel: UILabel {
    override func drawText(in rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()!
        context.stroke(self.bounds.insetBy(dx: 1.0, dy: 1.0))
        super.drawText(in: rect.insetBy(dx: 5.0, dy: 5.0))
    }
}

你可以试试,然后摇晃数字和其他细节,使它们看起来像你想要的。

答案 2 :(得分:0)

尝试使用此课程。我相信你会得到你想要的填充物。

class CustomLabel: UILabel {


   var padding:UIEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)

   required init(frame: CGRect, padding: UIEdgeInsets) {
      super.init(frame: frame)
      self.padding = padding
   }

   required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
   }

   override func drawText(in rect: CGRect) {
      let changedRect: CGRect = CGRect(x: (rect.origin.x + padding.left), y: (rect.origin.y + padding.top), width: (rect.size.width - (padding.left + padding.right)), height: (rect.size.height - (padding.top + padding.bottom)))
      super.drawText(in: changedRect)
   }

}

以下列方式实施。

    let label:CustomLabel = CustomLabel(frame: CGRect(x: 0, y: 20, width: self.view.frame.size.width, height: 60), padding: UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20))
    label.text = "Hello world, baby ldsnvldnvsdnds,nv,svn,vn,"
    label.textColor = UIColor.blue
    label.font = UIFont.boldSystemFont(ofSize: 15)
    label.textAlignment = NSTextAlignment.left
    label.layer.borderColor = UIColor.black.cgColor
    label.layer.borderWidth = 1
    self.view.addSubview(label)

我用这个设置了填充。如果它不起作用,请随时回复。随意建议编辑以使其更好:)

答案 3 :(得分:0)

我在我的应用中使用了这个解决方案。请检查以下代码。

class CustomLabel: UILabel {
        
        var textInsets = UIEdgeInsets.zero {
            didSet { invalidateIntrinsicContentSize() }
        }
        
        override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
            let textRect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
            let invertedInsets = UIEdgeInsets(top: -textInsets.top,
                                              left: -textInsets.left,
                                              bottom: -textInsets.bottom,
                                              right: -textInsets.right)
            return textRect.inset(by: invertedInsets)
        }
        
        override func drawText(in rect: CGRect) {
            super.drawText(in: rect.inset(by: textInsets))
        }
    }