简体   繁体   中英

Calculate Cell height on basis of label text + image

I have created a custom cell that have IMAGE view, and two label's the data of labels's are populated from a plist file, the data is populated properly but on front end the cell didn't show the data properly, the label cut's the data. I am using Uilabel view's.

Please have a view to my code, i have search over internet and followed some tutorial's as well but nothing work's.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    Customviewcell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    UIImage * image = [UIImage imageNamed:justThumbs[indexPath.row]];
    cell.CustomTitle.text=justTitles[indexPath.row];

    cell.CustomTitle.numberOfLines =0;
    [cell.CustomTitle sizeToFit];
    cell.CustomDes.text=justDesc[indexPath.row];
    cell.CustomDes.numberOfLines=0;
      [cell.CustomDes sizeToFit];
    [cell.CustomTitle layoutIfNeeded];
    [cell.CustomDes layoutIfNeeded];
    [cell layoutIfNeeded];
    cell.Customimage.image=image;
    return cell;
}

Code for calculating the height as per stackoverflow different question's answer's.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Calculate Height Based on a cell
    if (!self.customcell) {
      self.customcell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    }
    // Configure Cell
    UIImage * image = [UIImage imageNamed:justThumbs[indexPath.row]];
    self.customcell.CustomTitle.text=justTitles[indexPath.row];
    self.customcell.CustomTitle.numberOfLines=0;
    [self.customcell.CustomTitle sizeToFit];
    self.customcell.CustomDes.text=justDesc[indexPath.row];
    self.customcell.CustomDes.numberOfLines=0;
    [self.customcell.CustomDes sizeToFit];
    self.customcell.Customimage.image=image;

    //Layout Cell

    //Get Hieght for the cell

    if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
    {

        if ([[UIScreen mainScreen] bounds].size.height == 568)
        {

            CGRect frame = [NSString setAttributeWithString:self.customcell.CustomTitle.text withLineSpacing:0.2 withSize:CGSizeMake(270, 999999999) withFont:self.customcell.CustomTitle.font withLabel:self.customcell.CustomTitle setLabelTextColor:self.customcell.CustomTitle.textColor setTextAlignment:self.customcell.CustomTitle.textAlignment];
            self.customcell.CustomTitle.height.constant = frame.size.height;
            frame = [NSString setAttributeWithString:self.customcell.CustomDes.text withLineSpacing:0.3 withSize:CGSizeMake(150, 999999999) withFont:self.customcell.CustomDes.font withLabel:self.customcell.CustomDes setLabelTextColor:self.customcell.CustomDes.textColor setTextAlignment:self.customcell.CustomDes.textAlignment];
            self.customcell.CustomDes.height.constant = frame.size.height;



        }
        else{

            CGRect frame = [NSString setAttributeWithString:self.customcell.CustomTitle.text withLineSpacing:1 withSize:CGSizeMake(337, 999999999) withFont:self.customcell.CustomTitle.font withLabel:self.customcell.CustomTitle setLabelTextColor:self.customcell.CustomTitle.textColor setTextAlignment:self.customcell.CustomTitle.textAlignment];
            self.customcell.CustomTitle.height.constant = frame.size.height;
            frame = [NSString setAttributeWithString:self.customcell.CustomDes.text withLineSpacing:1 withSize:CGSizeMake(227, 999999999) withFont:self.customcell.CustomDes.font withLabel:self.customcell.CustomDes setLabelTextColor:self.customcell.CustomDes.textColor setTextAlignment:self.customcell.CustomDes.textAlignment];
            self.customcell.CustomDes.height.constant = frame.size.height;


        }
    }
    [self.customcell layoutIfNeeded];

       // CGFloat height = self.customcell.CustomTitle.height.constant+self.customcell.CustomDes.height.constant+189;
    CGFloat height = [self.customcell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;


    //Add padding of 1
    return height;
}

Used Github opensource library to solve the issue but didn't worked.

https://github.com/punchagency/line-height-tool

Issue still remain's, text of label's cut off, content hanging is at Required and Content is at 1000 horizontal + vertical..

Please help..

Thanks allot.

You can get the height of a string in certain bounds with the NSString method boundingRectWithSize, like

NSString* text = @"Test text";

CGRect textRect = [text boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX)
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                  attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]}
                                                     context:nil];
CGFloat textHeight = ceilf(textRect.size.height);

Use your own font and font size, and you can also add other attributes to the attributes dictionary if necessary.

Use following method to calculate your UILabel height:

add also use this method in your class.

- (CGFloat)getLabelHeight:(UILabel*)label{
CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX);
CGSize size;

NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [label.text boundingRectWithSize:constraint
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{NSFontAttributeName:label.font}
                                              context:context].size;

size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));

return size.height;}

Update your heightForRowAtIndexPath Method as per your requirement: calculate all UI Height and return.

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

Customviewcell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];

CGFloat totalHeight = 0;

cell.CustomTitle.text=justTitles[indexPath.row];
//get label Height
totalHeight += [Helper getLabelHeight:cell.CustomTitle];

cell.Customimage.image = [UIImage imageNamed:justThumbs[indexPath.row]];
CGFloat imageHeight = cell.Customimage.frame.size.height; //or Add image height here

totalHeight += imageHeight;

return totalHeight;}

在iOS8 +中,您可以使用自动调整大小的单元格: http : //www.appcoda.com/self-sizing-cells/

Auto Resizing of Cell is available in Ios 8.0, The issue my deployment target was ios 7.0, which is causing the layout issues.

Please refer to these articles:

http://www.appcoda.com/self-sizing-cells/

The Code is in swift but need to do same thing's in objective c as well.

This will also help's you.

http://useyourloaf.com/blog/self-sizing-table-view-cells.html

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