2013-12-20 60 views
0

我正在調整表的cellUILabel。標籤似乎被調整大小,但仍然在一行上。如何處理這個?調整UILabel不太正常

的標籤從故事板設置爲:

Line Breaks: Word Wrap; 
Autoshrink: Fixed Font Size; 
Lines: 0 

CGRect textRect = [cell.sampleLabel.text boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName:cell.sampleLabel.font} context:nil]; 

NSLog(@"textRect height: %f", textRect.size.height); 
cell.sampleLabel.frame = textRect; 
NSLog(@"label height: %f", cell.sampleLabel.frame.size.height); 

NSLog

2013-12-20 11:04:41.623 DevCloud[16613:70b] textRect height: 223.091019 
2013-12-20 11:04:41.624 DevCloud[16613:70b] label height: 224.000000 
2013-12-20 11:04:41.626 DevCloud[16613:70b] textRect height: 223.091019 
2013-12-20 11:04:41.627 DevCloud[16613:70b] label height: 224.000000 
+0

不相關的Xcode 5這只是一個IDE。 – Raptor

+0

是否可以用動態高度創建標籤? –

回答

1

試試這個

cell.lbl_view2_disc1.numberOfLines=1000; 
CGSize maximumLabelSize = CGSizeMake(260, FLT_MAX); 
CGSize expectedLabelSize = [cell.lbl_view2_disc1.text sizeWithFont:calibri constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByCharWrapping]; 

如果您的UILabel寬度修復然後設置固定寬度maximumLabelSize

CGSize maximumLabelSize = CGSizeMake(260, FLT_MAX); 

如果您的UILabel高度修復然後設置修復UIlabel.frame.size.height的高度maximumLabelSize

CGSize maximumLabelSize = CGSizeMake(FLT_MAX,cell.lbl_view2_disc1.frame.size.height); 

在我的代碼工作

0

嘗試

cell.sampleLabel.numberOfLines = 0 
0

嘗試

cell.sampleLabel.numberOfLines = 0 
[cell.sampleLabel sizeToFit]; 
+0

這不起作用。行數:0已經從故事板中設置。 – Aleksandrenko

1
#define FONT_SIZE 14.0f 
#define CELL_CONTENT_WIDTH 280.0f 
#define CELL_CONTENT_MARGIN 40.0f 




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

    {   
    NSString *text = @"your string"; 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN), 2000.0f); 

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]; 

    CGFloat height = MAX(size.height, 20.0f); 
    cellheight=height + (CELL_CONTENT_MARGIN * 2); 
    return height + (CELL_CONTENT_MARGIN * 2); 
    } 


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *CellIdentifier = @"Cell"; 
    UILabel *label = nil; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    for (UIView *view in cell.contentView.subviews) { 
     [view removeFromSuperview]; 
    } 

    NSString *text = @"your string"; 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN), 2000.0f); 

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]; 
    commentscontentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 290, MAX(size.height, 44.0f))]; 
    commentscontentView.tag=111; 
    [cell.contentView addSubview:commentscontentView]; 


    label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 220, 20)]; 
    [label setLineBreakMode:NSLineBreakByWordWrapping]; 
    [label setMinimumFontSize:FONT_SIZE]; 
    label.backgroundColor=[UIColor clearColor]; 
    [label setNumberOfLines:0]; 
    [label setFont:[UIFont fontWithName:@"OpenSans-Light" size:FONT_SIZE]]; 
    [label setTag:1]; 
    [commentscontentView addSubview:label]; 


    if (!label) 
     label = (UILabel*)[cell viewWithTag:1]; 

    [label setText:text]; 


    [label setFrame:CGRectMake(20, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN), MAX(size.height, 44.0f))]; 
    label.textColor=[UIColor blackColor]; 
    [label setBackgroundColor:[UIColor clearColor]]; 


    return cell; 
    } 
+0

如果要動態增加標籤的高度,則使用此項。在單元格配置中,我們動態地增加標籤高度和行高度,我們正在動態增加單元高度。 –