调整UILabel的大小不太合适

时间:2013-12-20 09:10:57

标签: ios objective-c uitableview uilabel

我正在调整表格cellUILabel的大小。该标签似乎已调整大小,但仍保留在一行。怎么处理这个?!

标签从故事板中设置为:

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

4 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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高度已修复,则在 maximumLabelSize

中设置UIlabel.frame.size.height的修复高度
CGSize maximumLabelSize = CGSizeMake(FLT_MAX,cell.lbl_view2_disc1.frame.size.height);

在我的代码中工作

答案 2 :(得分:0)

尝试

cell.sampleLabel.numberOfLines = 0

答案 3 :(得分:0)

尝试

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