UILabel dynamic tronquant le text

J'utilise le code fourni dans cette réponse pour créer une label dynamic et cela fonctionne pour la plupart. Mais chaque fois que le text de l'label dépasse 94 caractères, il est tronqué et les ellipses sont ajoutées.

Il y a une chose plus étrange à ce sujet est que si j'ajoute plus de caractères à la string ils sont montrés mais les 2 dernières lignes sont encore tronquées.

Par exemple.

La string:

this is a very very long ssortingng with lots of words to test the dynamic bubble sizing one two three. 

apparaît comme ceci:

 this is a very very long ssortingng with lots of words to test the dynamic bubble sizing one tw... 

Mais quand je double la string en utilisant de nouveau la même phrase dans l'label, elle montre plus de text mais la tronque toujours.

Par exemple.

La string:

 this is a very very long ssortingng with lots of words to test the dynamic bubble sizing one two three. this is a very very long ssortingng with lots of words to test the dynamic bubble sizing one two three. 

montre comme ceci:

 this is a very very long ssortingng with lots of words to test the dynamic bubble sizing one two three. this is a very very long ssortingng with lots of words to tes... 

Voici le code que j'utilise.

 NSSsortingng *temp = [NSSsortingng ssortingngWithFormat:@"this is a very very long ssortingng with lots of words to test the dynamic bubble sizing one two three"]; captionLabel.text = temp; //Calculate the expected size based on the font and linebreak mode of your label CGSize maximumLabelSize = CGSizeMake(296,9999); CGSize expectedLabelSize = [temp sizeWithFont:captionLabel.font constrainedToSize:maximumLabelSize lineBreakMode:captionLabel.lineBreakMode]; //adjust the label the the new height. CGRect newFrame = captionLabel.frame; newFrame.size.height = expectedLabelSize.height; captionLabel.frame = newFrame; 

J'espère que quelqu'un a une idée parce que cela me fait me gratter la tête.

MODIFIER

Utiliser captionLabel.frame.size.width au lieu de 296 codé en dur l'a corrigé, grâce à @troolee, s'il / elle choisit de créer une réponse, je la marquerai correctement.

J'espérais que @troolee aurait fait de son commentaire une réponse maintenant, mais comme il ne l'a pas fait, je vais postr la réponse et la corriger afin que je puisse fermer cette question.

L'utilisation de captionLabel.frame.size.width à la place de 296 codé en dur l'a corrigé.

Au lieu de captionLabel.lineBreakMode , écrivez simplement UILineBreakModeWordWrap . Ça devrait marcher.

  NSSsortingng * test=@"this is test this is test inthis is test ininthis is test inthis is test inthis is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel END"; testLabel.text = test; testLabel.numberOfLines = 0; //will wrap text in new line [testLabel sizeToFit]; [self.view addSubview:testLabel]; 

Cela va sûrement vous aider.

Merci

Essayez la catégorie UILabel suivante. Merci pour le créateur.

UILabel + VAlign.h

 #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> @interface UILabel (VAlign) - (void) setVerticalAlignmentTopConstrainedToSize:(CGSize)size; @end 

UILabel + VAlign.h

 #import "UILabel+VAlign.h" @implementation UILabel (VAlign) - (void) setVerticalAlignmentTopConstrainedToSize:(CGSize)size { CGSize textSize = [self.text sizeWithFont:self.font constrainedToSize:size lineBreakMode:self.lineBreakMode]; CGRect textRect = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, textSize.height); [self setFrame:textRect]; [self setNeedsDisplay]; } @end