Pregunta

Estoy tratando de mostrar una larga parte de texto dentro de una UILABEL en una línea. La UILABEL es una subvencora de UiscrollView para que pueda desplazarse y ver a toda la UILABEL.

Mi problema es que el método SIZETOFIT solo funciona parialmente.

textLabel.attributedText = attributedString;
textLabel.numberOfLines = 1;
[textLabel sizeToFit];
textScrollView.contentSize = CGSizeMake(textLabel.frame.size.width, textLabel.frame.size.height);

El tamaño del contenido de UiscrollView se obtiene lo suficientemente grande como para mostrar todo lo ílaco, pero para una línea como:

Así que intentaré compartir algunos de ellos aquí de vez en cuando ".

The Uilabel Shows:

Así que intentaré compartir SOM ...

¿Qué estoy haciendo mal?

¿Fue útil?

Solución

Resulta que el código está bien, pero se verificó el uso automático. Sin marcarlo, todo funciona simplemente genial ...

Otros consejos

If you want to achieve this with auto layout turned on it's simple. Just make sure you add numberOfLines

textLabel.adjustsFontSizeToFitWidth = YES;
textLabel.numberOfLines = 0;

Surprisingly, if you did not put a constraint on the label's width, this would work:

[textLabel.superview layoutSubviews];

I learned this by trial and error.

try

textLabel.adjustsFontSizeToFitWidth  = YES;
textLabel.minimumFontScale      =  0.5;  

Since you have restricted your Label to show only one line of Text and truncate the rest , it is behaving the same

textLabel.attributedText = attributedString;
textLabel.numberOfLines = 0;
[textLabel sizeToFit];
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textScrollView.contentSize = CGSizeMake(textLabel.frame.size.width, textLabel.frame.size.height);

Hope it will help you

The most common reason for sizeToFit not working properly is the UILabel not having any autolayout constraints, for instance if you're implicitly relying on the view position remaining fixed relative to the top left. Adding any constraint at all (leading, top, centerY, anything) will fix it, presumably because it will result in layoutSubviews being called at some point, as suggested in Maxthon Chan's answer.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top