简体   繁体   中英

UILabel with dynamic height into UITableviewcell

I am developing an app which required to display UILabel into UITableviewCell . I also need to resize UILabel as per text size. I am using following code for get contentsize of text size.

CGRect rect = [as boundingRectWithSize:CGSizeMake(220.0, 2000.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil];

For update frame of UILabel I use following code.

 rect.origin.x = cell.lblDescription.frame.origin.x;
 rect.origin.y = cell.lblDescription.frame.origin.y;
 rect.size.width = cell.lblDescription.frame.size.width;

 [cell.lblDescription setFrame:rect];

It set wrong frame. Please find attached screenshot. 在此输入图像描述

You have to check the height of the label in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath UITableViewDelegate Method.

CGSize maximumLabelSize = CGSizeMake(your_label_width, FLT_MAX);
CGSize expectedLabelSize = [label_text sizeWithFont:[UIFont fontWithName:@"your_font" size:your_font_size] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];

then set the cell height as expectedLabelSize.height .

also do the same thing in your custom cell.

    NSString *text = [NSString stringWithFormat:@"%@",[[arr_cart objectAtIndex:indexPath.row] objectForKey:@"name"]];
    UIFont *font = [UIFont fontWithName:@"ArialMT" size:12];
    CGSize size = [(text ? text : @"") sizeWithFont:font constrainedToSize:CGSizeMake(200, 9999) lineBreakMode:NSLineBreakByWordWrapping];
    UILabel *lbl_desc=[[UILabel alloc]init];
    lbl_desc.numberOfLines = 0;
    lbl_desc.frame=CGRectMake(70,18, size.width, size.height);
    lbl_desc.lineBreakMode = NSLineBreakByWordWrapping;
    lbl_desc.text = (text ? text : @"");
    lbl_desc.font = font;
    lbl_desc.backgroundColor=[UIColor clearColor];
    lbl_desc.textColor = [UIColor darkTextColor];
    [cell.contentView addSubview:lbl_desc];
    [lbl_desc release];

Use following code

[self setDynamicHeightOfLabel:lblName withLblWidth:97 andFontSize:13]; 

In above method you just need to pass your UILabel name with specific width and fontSize of label.

-(void) setDynamicHeightOfLabel:(UILabel *) myLabel withLblWidth:(CGFloat) width andFontSize:(int) fontSize
{
    CGSize myLabelSize = CGSizeMake(width, FLT_MAX);
    CGSize expecteingmyLabelSize = [myLabel.text sizeWithFont:myLabel.font constrainedToSize:myLabelSize lineBreakMode:myLabel.lineBreakMode];
    CGRect lblFrame = myLabel.frame;
    lblFrame.size.height = expecteingmyLabelSize.height;
    myLabel.frame = lblFrame;
    int addressLine = myLabel.frame.size.height/fontSize;
    myLabel.numberOfLines = addressLine;
}

Using above code you can set dynamic height of UILabel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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