sizeWithFont:constraintedToSize: is deprecated, but boundingRectWithSize not working for me

337 Views Asked by At

Ok so heres my dilemma, I have a block of code that works great, its deprecated but it calculates the appropriate size of the height of any block of text. (Below works but deprecated)

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

if (indexPath.section == 0 && indexPath.row == 2) {

   CGSize size = [[item desc] sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290 - (10 * 2), 200000.0f)];

    CGFloat height = MAX(size.height, 44.0f);

    return height + (2 * 2);
}
return 44;
}

Trying to remove deprecated code so I've been stuck on the proper way to fix it for a few days now and I came up with this.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 2) {

    NSStringDrawingContext *ctx = [NSStringDrawingContext new];
    NSAttributedString *aString = [[NSAttributedString alloc] initWithString:[item desc]];
    UITextView *calculationView = [[UITextView alloc] init];
    [calculationView setAttributedText:aString];
    CGRect textRect = [calculationView.text boundingRectWithSize:self.view.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:calculationView.font} context:ctx];

    CGFloat height = MAX(textRect.size.height, 44.0f);
    return height + (2 * 2);

}
return 44;
}

This certainly does not work it works from a code level standpoint but it does not size the cell appropriately. I feel like this should be easy any help appreciated.

2

There are 2 best solutions below

1
On

Try this:

CGRect textRect = [calculationView.text boundingRectWithSize:CGSizeMake(290 - (10 * 2), 200000.0f) 
                        options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:calculationView.font} context:ctx];

You have not provided required size constraint as you were providing in deprecated method.. That's why you are not getting desired output..

1
On

Just edit heightForRow method like :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 2)
    {
        NSStringDrawingContext *ctx = [NSStringDrawingContext new];
        NSAttributedString *aString = [[NSAttributedString alloc] initWithString:[item desc]];
        UITextView *calculationView = [[UITextView alloc] init];
        [calculationView setAttributedText:aString];
        [calculationView sizeToFit];
        //CGRect textRect = [calculationView.text boundingRectWithSize:self.view.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:calculationView.font} context:ctx];

        CGFloat height = calculationView.frame.size.height; //MAX(textRect.size.height, 44.0f);
        NSLog(@"%.2f", calculationView.frame.size.height);
        return height + (2 * 2);
    }
    return 44;
}