2012-07-19 137 views
4

我使用this answer中提供的代码来创建动态标签,并且它在大多数情况下都能正常工作。但是,只要标签文本长度超过94个字符,它就会被截断并省略“。动态UILabel截断文本

还有一件更奇怪的事情是,如果我向字符串添加更多字符,它们会显示,但最后2行仍然会被截断。

例如,

字符串:

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... 

但是当我通过在标签再次使用同一个句子一倍字符串时,它表现出更多的德文本,但仍将截断它。

例如,

字符串:

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,如果他/她选择创建一个答案,我将迎来它是正确的。

+0

看看这个! HTTP://计算器。com/questions/9059631/autoshrink-on-a-uilabel-with-multiple-lines/9060833#9060833 – 2012-07-19 10:35:23

+2

如果您将使用captionLabel.frame.size.width而不是硬编码的'296',该怎么办?也许标签的宽度不是'296'? – 2012-07-19 10:43:06

+0

captionLabel.frame.size.width修复了它,并将其作为答案粘贴,并将其标记为正确。似乎再次我已经被一些复制面食代码... – triggs 2012-07-19 10:46:43

回答

1

我一直希望@troolee现在可以让他的评论成为一个答案,但既然他没有,我会发布答案并将其标记为正确,以便我可以关闭此问题。

使用captionLabel.frame.size.width而不是硬编码296修复它。

1
NSString * [email protected]"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]; 

这一定会帮助你。

感谢

+0

我已编辑我的代码,这次肯定会帮助你:)谢谢 – SALMAN 2012-07-19 10:45:51

0

请尝试以下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 
1

相反的captionLabel.lineBreakMode,只写UILineBreakModeWordWrap。它应该工作。