Question

J'essaie de montrer un long morceau de texte à l'intérieur d'un Uilabel dans une ligne. L'Uilabel est une sous-vision de Uiscrollview pour que je puisse faire défiler et voir l'Uilabel entier.

Mon problème est que la méthode Sizetofit ne fonctionne que partiellement.

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

La taille du contenu UiscrollView est suffisamment importante pour afficher la totalité de l'usure, mais pour une ligne comme:

Je vais donc essayer de partager certains d'entre eux de temps en temps. "

L'Uilabel montre:

Je vais donc essayer de partager SOM ...

Qu'est-ce que je fais mal?

Était-ce utile?

La solution

tourne le code est tout simplement bien - mais l'utilisation automatique a été vérifiée. Non contrôlé - tout fonctionne bien ...

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top