2011-05-20 61 views
6

我需要插入到UILabel多行文字。我做到以下几点:Multiline UILabel?

NSMutableString * spName = [[NSMutableString alloc ]initWithString:@""]; 

for (NSUInteger i=0; i<arrEx.count; ++i) 
{ 
    ExInfo * exInf = [arrEx objectAtIndex:i]; 
    [spName appendString:[MyObject getName:exInf.spNum]]; 
    [spName appendString:@" "]; 
    [spName appendString:exInf.totalTime]; 
    [spName appendString:@"\n"];   
} 

CGSize size = [spName sizeWithFont:[UIFont systemFontOfSize:14] 
       constrainedToSize:constraint 
        lineBreakMode:UILineBreakModeWordWrap]; 

[cell.exsInfoLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, top, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), size.height)]; 
[cell.exsInfoLabel setText:spName]; 
[spName release]; 

arrEx包括两个项目,所以它应该是两个字符串。但UITableViewCell只包含第一个字符串。 在IB中,我为UILabel cell.exsInfoLabel设置了行数为0。

回答

10

试试这个:的setText之前

CGSize labelsize; 
UILabel *commentsTextLabel = [[UILabel alloc] init]; 
[commentsTextLabel setNumberOfLines:0]; 
[commentsTextLabel setBackgroundColor:[UIColor clearColor]]; 
NSString *text = @"yourtextString"; 
[commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]]; 
labelsize = [text sizeWithFont:commentsTextLabel.font 
      constrainedToSize:CGSizeMake(268, 2000.0) 
       lineBreakMode:UILineBreakModeWordWrap]; 
commentsTextLabel.frame = CGRectMake(10, 24, 268, labelsize.height); 
[cell.contentView addSubview:commentsTextLabel]; 
[commentsTextLabel release]; 
+0

根据您的要求修改的框架。 – Gypsa 2011-05-20 10:26:48

+0

嗨,我试图setNumberOfLines设置为0.它不起作用我我理解你的帖子? – 2011-05-20 11:05:35

+5

行数0意味着您可以添加无限数量的行。苹果文档说: - 该属性控制使用的最大行数,以便将标签的文本放入其边界矩形中。此属性的默认值为1.要删除任何最大限​​制并根据需要使用尽可能多的行,请将此属性的值设置为0. – Gypsa 2011-05-20 11:19:49

2

尝试:

cell.exsInfoLabel.numberOfLines = 2; 

或者:

cell.exsInfoLabel.numberOfLines = arrEx.count; 
+0

这不幸没有工作:( – 2011-05-20 10:53:15