dynamicUILabel截断文本

我使用这个答案中提供的代码来创build一个dynamic标签,它大部分工作。 但是,每当标签文本超过94个字符的长度,它会被截断,省略号被添加。

还有一个更奇怪的事情是,如果我添加更多的字符到string,他们显示,但最后2行仍然被截断。

例如。

string:

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

如下所示:

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

但是,当我在标签中再次使用相同的句子来加倍string时,它会显示更多的te文本,但仍会截断它。

例如。

string:

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

如下所示:

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

这是我正在使用的代码。

 NSString *temp = [NSString stringWithFormat:@"this is a very very long string 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; 

希望有人有一个想法,因为这让我挠了脑袋。

编辑

使用captionLabel.frame.size.width而不是硬编码的296固定它,感谢@troolee,如果他/她select创build一个答案,我会标记它是正确的。

我希望@troolee现在可以给他的评论一个答案,但是既然他没有,我会发布答案,并将其标记为正确的,所以我可以closures这个问题。

使用captionLabel.frame.size.width而不是硬编码的296固定它。

而不是captionLabel.lineBreakMode ,只需编写UILineBreakModeWordWrap 。 它应该工作。

  NSString * 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]; 

这一定会帮助你。

谢谢

尝试以下UILabel类别。 感谢创造者。

的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