I have searched the solution to this in the past QAs, but could not find the right one.
Does anyone know how to adjust aUILabel size dynamically to fit the
This is quite easy to do with Auto Layout. No need to do anything in code.
Use auto layout to pin each label's top and left edges only. Don't add constraints for the width and height. The view's intrinsic content size will take care of that.
Here is what the constraints look like:
There is nothing at all special about the code. No need to use sizeToFit or anything like that.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var labelOne: UILabel!
@IBOutlet weak var labelTwo: UILabel!
@IBOutlet weak var labelThree: UILabel!
@IBAction func changeTextButtonTapped(_ sender: UIButton) {
labelOne.text = "David"
labelTwo.text = "met"
labelThree.text = "her"
}
}