Resize label to fit text amount - Swift Resize label to fit text amount - Swift xcode xcode

Resize label to fit text amount - Swift


You'll want to do this:

myLabel.sizeToFit()

As seen here: https://developer.apple.com/documentation/uikit/uiview/1622630-sizetofit

This will update the label's frame to fit the content. You can then place it or make any edits after this that you want.


Here's the step that work in mine:

  1. Set width constraint to the label, then click the constraint.
  2. Select Size Inspector.
  3. Set the relation to less than or equal, and set max width.

enter image description here


sizeToFit() doesn't always work so you should use this:

myLabel.adjustsFontSizeToFitWidth = truemyLabel.minimumScaleFactor = 0.5

Or if you don't have limit lines you can do this:

myLabel.numberOfLines = 0