简体   繁体   中英

How to dynamically change cell content label height according to text in tableview

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


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section == 0) {

        cell.accessoryType = UITableViewCellAccessoryNone;

        if (indexPath.row == 0) {

            cell.textLabel.text = NSLocalizedString(@"Username", @"");
            account = [self createNewLabel];
            account.text = @"this is xyz doing some work in xyz, xyz is also working with me and  we are doing great work, We hope to continue good work in future and make this company in good position. I wanted to stayed in this company for long time to take  this company in high value";
            [cell.contentView addSubview:account];

        }
        if (indexPath.row == 1) {

            cell.textLabel.text = NSLocalizedString(@"Password", @"");
            contact = [self createNewLabel];
            [cell.contentView addSubview:contact];
        }
    }
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
    cell.textLabel.textColor = [UIColor blackColor];

    return cell;
}

How do i adjust account label text in cell content view, there will be many sections and many data differently

Here you go

+ (CGFloat) getHeightOfString:(NSString*)string withSize:(CGFloat)size andWidth:(CGFloat)width
{
    CGSize boundingSize = CGSizeMake(width, CGFLOAT_MAX);

    CGSize stringSize = [string sizeWithFont:[UIFont systemFontOfSize:size] constrainedToSize:boundingSize  lineBreakMode:UILineBreakModeWordWrap];
    return stringSize.height;
}

The above method gives you height of a text, this height you can set to your label in cell. The method requires the width of the label, font size and the text.

For implementing the label heights dynamically you need to take care of height of the row. So while setting the rowHeight, in the delegate method of tableView you need to calculate the height over their and set. And also in cellForRow method you need to calculate the height and set.

You also need to calculate the number of lines dynamically, it will be usually

int numberOfLines = height/18; // or try height/19

It should work perfectly.

Thanks

Try this:

NSString *lblText = @"YOUR STRING";
    CGSize lblSize = [lblText sizeWithFont:[UIFont italicSystemFontOfSize:17] constrainedToSize:CGSizeMake(930, 500) lineBreakMode:UILineBreakModeWordWrap];
    CGRect frame = account.frame; 
        frame.size.height = lblSize.height;
        account.frame = frame;

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