2013-02-19 26 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]; 
相關問題