0

Right now, the edges of the label line up exactly with the end of the text - I want to give the text some padding(?) or space so that you still see some of the background. Is this a constraints issue?

let lblTitle: UILabel = {
    let lbl=UILabel()
    lbl.text="Try this quiz."
    lbl.textColor=UIColor.black
    lbl.textAlignment = .justified
    lbl.font = UIFont(name: "Gotham", size: 50)
    lbl.backgroundColor = UIColor(white: 1, alpha: 0.8)
    lbl.adjustsFontSizeToFitWidth = true
    lbl.layer.masksToBounds = true
    lbl.numberOfLines=0
    lbl.sizeToFit()
    lbl.layer.cornerRadius = 5
    lbl.translatesAutoresizingMaskIntoConstraints=false
    lbl.minimumScaleFactor = 0.1
    return lbl
sheishistoric
  • 85
  • 1
  • 9

1 Answers1

0

You probably should just use constraints (ie put the label in a container and give it space to to container equal to the padding you want, and only give the container 2 position constraints so it infers height and width from the label + padding), but if not its easy enough to do by adding a negative inset to the frame right after you call lbl.sizeToFit. For instance this adds 10 padding points in every direction:

lbl.frame.insetBy(dx: -20, dy: -20)
Josh Homann
  • 15,933
  • 3
  • 30
  • 33