iOS calculate text height in tableView cell

前端 未结 2 1986
深忆病人
深忆病人 2020-12-10 14:00

i\'m currently developing on an app, which displays some tweets in a tableview. On the storyboard i created a prototype-cell, which includes the basic gui concept of a tweet

相关标签:
2条回答
  • 2020-12-10 14:44

    Try this for your issue:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSDictionary * currentTweet = [tweetArray objectAtIndex: indexPath.row];
    
        NSString * tweetTextString = [currentTweet objectForKey: @"text"];
    
        CGSize textSize = [tweetTextString sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:CGSizeMake(240, 20000) lineBreakMode: UILineBreakModeWordWrap]; //Assuming your width is 240
    
        float heightToAdd = MIN(textSize.height, 100.0f); //Some fix height is returned if height is small or change it to MAX(textSize.height, 150.0f); // whatever best fits for you
    
        return heightToAdd;
    }
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-10 14:49

    If you are looking for the iOS 7 answer I ran across it here:

    iOS 7 sizeWithAttributes: replacement for sizeWithFont:constrainedToSize

    0 讨论(0)
提交回复
热议问题