Uilabel border and padding

I want to add a registration inside my label in order to have spaces between it and its border. I created a class for this 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 myLabel type from UILabel to UILabelPadding. In my UIViewController, I set the text myLabel, called sizeToFit (), after which I add the border and 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

Added border and background color, but padding does not work. When I debug, sizeThatFits () is never called.

Any help please?

+6
source share
3 answers

I solved the problem!

For those facing the same problem:

1- Create a class extending from 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: rect.inset(by: 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 , .

+20

, , . :

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

. , , .

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)

, , . , . , :)

0

Source: https://habr.com/ru/post/1659743/


All Articles