繁体   English   中英

调整UILabel的大小不太合适

[英]Resizing UILabel not quite working

我正在调整表格的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
#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;
    }

试试这个

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);

在我的代码工作

尝试

cell.sampleLabel.numberOfLines = 0

尝试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM