Hey :) I have a label and I need to make the width of that label smaller or larger accourding to the text amount, and I found only how to adjust the text to fit the size but the how to adjust the size to fit the text, any ideas ?
Set width constraint to the label, then click the constraint. Select Size Inspector. Set the relation to less than or equal, and set max width.
A view that displays one or more lines of informational text.
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.
sizeToFit() doesn't always work so you should use this:
myLabel.adjustsFontSizeToFitWidth = true
myLabel.minimumScaleFactor = 0.5
Or if you don't have limit lines you can do this:
myLabel.numberOfLines = 0
Here's the step that work in mine:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With