Auto-size label to fit text

suggest change

This example shows how a label’s width can automatically resize when the text content changes.

Pin the left and top edges

Just use auto layout to add constraints to pin the left and top sides of the label.

After that it will automatically resize.

Notes

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var myLabel: UILabel!

    @IBAction func changeTextButtonTapped(sender: UIButton) {
        myLabel.text = "my name is really long i want it to fit in this box"
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents