Resizing UILabel not quite working

做~自己de王妃 提交于 2019-12-13 06:25:45

问题


I'm resizing a table's cell and a UILabel. The label seems to be resized, but remains on one line. How to deal with this?!

The label is set from the storyboard as:

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

The 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

回答1:


try this one

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

if your UIlabel width fix then set fix width in maximumLabelSize

CGSize maximumLabelSize = CGSizeMake(260, FLT_MAX);

if your UIlabel height fix then set fix height of UIlabel.frame.size.height in maximumLabelSize

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

in my code working




回答2:


#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;
    }



回答3:


Try

cell.sampleLabel.numberOfLines = 0



回答4:


Try

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


来源:https://stackoverflow.com/questions/20699974/resizing-uilabel-not-quite-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!