Search code examples
iosswift3uilabel

Dynamically adjust width of UILabel in Swift 3


I'm trying to get my UILabel to get wider (along with its border and background color) as the content gets more - and then less when the content is reduced.

Where do I go to get started, I've looked at the Attributes Inspectors and it looks like this can only be done with code (which I'm fine with).

I thought adding two labels in a horizontal stack would do the trick, but it doesn't update in real-time (it will update the label only on launch).


Solution

  • Try using :

    myLabel.sizeToFit()
    

    on your label.This should update the label's frame to fit the content.