Detect when a text does not fit in a UI Label?

0

In my app I put a UILabel and inside I put a text, but apparently the text is longer than it can fit, and I would like to know if you can detect when a text is too long, so you can download the size of the font using code with while or something like that.

Something like that is what I'm looking for.

while label.textoMuyLargo {
   label.sizeFont = label.sizeFont -1
}

How do you detect when a text is too long for a UI Label?

    
asked by Jose Fabio 웃 22.02.2017 в 19:06
source

2 answers

1

It is not necessary to change the size of the font by hand. For that you can use the property adjustsFontSizeToFitWidth of UILabel .

For example, this code:

label.numberOfLines = 1
label.minimumScaleFactor = 0.5
label.adjustsFontSizeToFitWidth = true
label.lineBreakMode = .byWordWrapping

The property minimumScaleFactor is used to calculate the minimum size of the font . That is, the size of it is reduced to a minimum of label.minimumScaleFactor * label.font.pointSize . For example, with minimumScaleFactor of 0.5 and one font of size 16px , the minimum size used will be 8px .

    
answered by 22.02.2017 / 19:15
source
2

You'd better set (from the builder interface the following properties

numberOfLines = 0

And with autolayout the Height with priority 250 .

That will make the label grow according to the size of the text. It is much cleaner and simpler. Try to avoid UI code in the controller .

    
answered by 09.06.2017 в 19:07