7

I have in my swift application some labels that should have border. I added border to my label but I want to have more spaces between the label and its border.

How can I do that?

Ne AS
  • 1,490
  • 3
  • 26
  • 58
  • Where do you want to add the space? Left padding, right padding? – Lizza Nov 02 '16 at 16:52
  • http://stackoverflow.com/questions/3476646/uilabel-text-margin ? – Larme Nov 02 '16 at 16:53
  • left, right, top and bottom – Ne AS Nov 02 '16 at 16:53
  • @Larme I already find this but when I added this to my code: `override func drawTextInRect(rect: CGRect) { let insets = UIEdgeInsets.init(top: 5, left: 5, bottom: 5, right: 5) super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets)) }` it gives me "Method does not override any method from its super class" and also "Value of type "UIViewController" has no member drawTextInRect" – Ne AS Nov 02 '16 at 16:58
  • It's a subclass of `UILabel` not of `UIViewController`. – Larme Nov 02 '16 at 16:59
  • So how it can be accepted inside my UIViewController? – Ne AS Nov 02 '16 at 17:00
  • wrap it in a view – Sulthan Nov 02 '16 at 17:04

4 Answers4

5

Try this:

myLabel.frame.size.width = myLabel.intrinsicContentSize.width + 10
myLabel.frame.size.height = myLabel.intrinsicContentSize.height + 10
myLabel.textAlignment = .center
Rob
  • 2,649
  • 3
  • 27
  • 34
5

A simple solution would be to embed your label inside a view, and then add the border to this view, instead of to the label itself. I hope this help you.

Reynaldo Aguilar
  • 1,906
  • 1
  • 15
  • 29
1

1. Add this class

PaddingLabel.swift

import UIKit

class PaddingLabel: UILabel {

    var edgeInset: UIEdgeInsets = .zero

    override func drawText(in rect: CGRect) {
        let insets = UIEdgeInsets.init(top: edgeInset.top, left: edgeInset.left, bottom: edgeInset.bottom, right: edgeInset.right)
        super.drawText(in: rect.inset(by: insets))
    }

    override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(width: size.width + edgeInset.left + edgeInset.right, height: size.height + edgeInset.top + edgeInset.bottom)
    }
}

2. change the class of your label to PaddingLabel

enter image description here

3. Add this code to your ViewController

import UIKit

class LabelViewController: UIViewController {

    @IBOutlet weak var label: PaddingLabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //Setting the border
        label.layer.borderWidth = 1
        label.layer.borderColor = UIColor.blue.cgColor
        
        //Setting the round (optional)
        label.layer.masksToBounds = true
        label.layer.cornerRadius = label.frame.height / 2
    
        //Setting the padding label
        label.edgeInset = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
    
    }
}

Result

enter image description here enter image description here

kyeahen
  • 161
  • 8
0

I did this (to get the text on the same place):

//label position
myLabel.layer.frame.size.width += 10
myLabel.layer.frame.origin.x -= 5
myLabel.textAlignment = .center

//border
myLabel.layer.borderColor = UIColor.systemTeal.cgColor
myLabel.layer.borderWidth = 1.0
label.layer.cornerRadius = 8.0