Calculate UITableViewCell height to fit string

前端 未结 2 1644
悲&欢浪女
悲&欢浪女 2020-12-07 05:42

I have a NSMutableArray, which could contain several hundreds of different strings. Each string is user defined and could have any length, yet no more than an a

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 06:26

    Use the following code to calculate and to set the height for cell:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Define `veryverySmallTitleFont`
        // Define `descLabel`
    
        NSString *ourText = @"Your string data from array";
        UIFont *font = [UIFont fontWithName:@"Verdana" size:veryverySmallTitleFont];
        font = [font fontWithSize:veryverySmallTitleFont];
    
        CGSize constraintSize = CGSizeMake(descLabel.frame.size.width, 1000);
        CGSize labelSize = [ourText sizeWithFont:font
                               constrainedToSize:constraintSize
                                   lineBreakMode:UILineBreakModeWordWrap];
    
        return labelSize.height;
    }
    

提交回复
热议问题