Search code examples
iosswiftuilabel

How to get the correct width of a UILabel after the text has been set?


I'm getting the width of a UILabel [after the text has been set] with:

myUILabel.frame.width

but its always the same width, no matter if it holds the String: "Hello." or the String: "Hello everyone on this planet.". But the UILabel needs to have a bigger width with the second String.


Solution

  • Try myUILabel.intrinsicContentSize(). Make sure you set your font first, of course.