2013-02-19 22 views
4

全部,UILabel numberOfLines在单元格内不起作用

我对iphone编程非常新颖。在下面的代码中,我希望文本显示注释标签中的所有文本,但现在它正在截断它。 numberofLines也无法正常工作。现在它正在这样做。 “我的名字是弗雷德,我没死......”但我想让它显示全文“我的名字是弗雷德,我还没死,所以让我活下去”,即使它必须在多行上。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return 80.0; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = nil; 

cell = [self.allCells objectForKey:[NSNumber numberWithInt:indexPath.row]]; 

if(!cell) 
{ 
    cell = [[[NSBundle mainBundle] loadNibNamed:@"UserCell2" owner:nil options:nil] lastObject]; 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    [self.allCells setObject:cell forKey:[NSNumber numberWithInt:indexPath.row]]; 
} 

GSAsynImageView *imgView = (GSAsynImageView*)[cell viewWithTag:1000]; 
UILabel *lblTitle = (UILabel*)[cell viewWithTag:1001]; 
UILabel *lblComment = (UILabel*)[cell viewWithTag:1003]; 
UILabel *lbltime = (UILabel*)[cell viewWithTag:1004]; 

//lblComment setLineBreakMode:NSLineBreakByWordWrapping]; 
//lblComment.numberOfLines = 0; 
//lblComment.lineBreakMode = UILineBreakModeCharacterWrap; 

if(self.arrComments.count==0) 
{ 
    imgView.hidden = YES; 
    lblTitle.text = nil; 
    lblComment.text = nil; 
    lbltime.text = nil; 
    if(indexPath.row==1)lblTitle.text = @"No comments yet"; 
} 
else 
{ 
    imgView.hidden = NO; 

    NSDictionary *dcUser = [self.arrComments objectAtIndex:indexPath.row]; 

    NSString *strBio = [dcUser objectForKey:@"CommentTxt"]; 
    NSString *strDisplayName = [dcUser objectForKey:@"CommenterDisplayName"]; 

    NSString *imgName = [dcUser objectForKey:@"ImageName"]; 
    NSString *usernamex = [dcUser objectForKey:@"CommenterUserName"]; 

    if([imgName isKindOfClass:[NSString class]]) 
    { 
     if([imgName rangeOfString:@"facebook"].location!=NSNotFound || [imgName rangeOfString:@"twimg"].location!=NSNotFound) 
      [imgView loadImageFromPath:imgName]; 
     else 
      [imgView loadImageFromPath:[NSString stringWithFormat:@"%@images/%c/%@/50x50%@",WEBSERVER,[usernamex characterAtIndex:0],usernamex,imgName]]; 
    } 

    lblTitle.text = strDisplayName; 
    lblComment.text = strBio; 
    lbltime.text = [self getDateTitle:[dcUser objectForKey:@"Date"]]; 

} 

return cell; 
} 
+1

您应该使用的UITextView显示多行代替的UILabel – 2013-02-19 05:12:02

+1

确保您的标签有足够的垂直空间,或即使设置了多行,它也会截断它。 – borrrden 2013-02-19 05:12:35

+0

增加标签的宽度,然后选中否。线= 0 ... – Vishal 2013-02-19 05:14:22

回答

3

试试这个娄代码并添加你的..

UILabel * lblTitle = [[UILabel alloc]init]; 
[lblTitle setFrame:CGRectMake(110, 31, 200, 50)];   
lblTitle.text = @"your Text "; 
lblTitle.lineBreakMode = UILineBreakModeWordWrap;// add this line 
lblTitle.numberOfLines = 0;// add this line 
lblTitle.font = [UIFont fontWithName:@"Helvetica" size:12]; 

更多细节请参见我的博客这个职位从THIS链接

+0

'UILineBreakModeWordWrap'现在已被弃用 - 使用'NSLineBreakByWordWrapping'来代替。 – thegrinner 2015-07-08 12:28:40

+0

在Swift3中使用.byWordWrapping – 2016-10-12 21:09:47

0

我想这可能是因为标签上的串你想要显示的大小超过了标签

0

可以使用的UILabel的特性,以增加行数的大小。 像UILabel* para = [[UILabel alloc]init];

para.numberoOfLines = 10; 

,并尝试改变para.lineBreakMode

0

做,这是在故事板或XIB最简单的方法。当你有一个表格视图时,你可以添加你的标签(和其他任何你想要的)到你自动获得的自定义单元格中。确保您的标签具有特定的宽度设置,并且对单元格的顶部和底部有约束(确保将行数设置为0)。如果你有这些,标签将随着你在tableView:heightForRowAtIndexPath:中设置的单元格的高度展开。

0

增加标签的高度,使线路的数量到2为特定的标签。

试试这个代码,

lblComment.numberofLines = 2; 
[lblComment setFrame:CGRectMake(100,20,200,70)]; 
1

试试这个...

UILabel * label = [[UILabel alloc]init]; 
    [label setFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)]; 
    label.text = @" Text to be displayed in label "; 
    label.lineBreakMode = UILineBreakModeWordWrap ;// Wrap at word boundaries 
    label.numberOfLines = 0;// this line include multiple lines 
    [cell addSubview:label]; 
相关问题