Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8383653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:12:52+00:00 2026-06-09T17:12:52+00:00

In my case, how would I resize a UITableViewCell to accomodate the height of

  • 0

In my case, how would I resize a UITableViewCell to accomodate the height of a UITextView that it contains?

I’m currently trying to achieve this using the following code:

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

    // init the font that we use
    UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16];

    if (indexPath.section == 0) {

        // get the text size of the thread
        CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];

        return textSize.height + 20;

    }

    // default
    return 60;

}

I feel like if I could just get access to the UITableViewCell in this method, then I could use the contentSize property of the UITextView in it, and return that. But I don’t think that’s possible at this point in the execution flow.

I can’t just tweak that buffer, because it doesn’t scale with the text size being returned by the get size function. The shorter the text, the more visible a larger buffer size is.

This is adjusting the height of the UITableViewCell, but it’s not showing all of the UITextView inside of it. I’ve specified the width of the UITextView in the sizeWithFont:constrainedToSize:lineBreakMode: method, which is 280. I’ve specified that I wish to have essentially no maximum height, so that it can word-wrap as much as it needs to, using the CGFLOAT_MAX constant. I append a buffer of 20 units to the resulting height of the method call, because there is a 20 unit buffer above the UITextView, in my Storyboard, and I’d like to also have a 20 unit buffer below it.

The end result of all of this still has text being clipped off, though. Here is a picture of what I am talking about:

clipped cell contents

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:12:53+00:00Added an answer on June 9, 2026 at 5:12 pm

    I’m going to go ahead and answer my question. The answer to this comes in two steps.

    First, adjust the size of the UITextView during your logic inside of the tableView:cellForRowAtIndexPath: method. Make sure to do this after making any changes to the text in the UITextView. I re-size my UITextView using the following method, inside of a subclass of UITableViewCell where self.text is the text view:

    - (void)resizeTextView {
    
        // get the current frame size
        CGRect frame = self.text.frame;
    
        // set it to the height of the text view
        frame.size.height = self.text.contentSize.height;
    
        // set the new size of the text view
        self.text.frame = frame;
    
    }
    

    Next, I specify the height of the UITableViewCell inside of the tableView:heightForRowAtIndexpath: method. This is where most of the mystery exists, because to get the height for this row you have to calculate the height of any text that is going to be displayed in the row, along with anything else you need to calculate the height of. In my scenario, is was a UITextView. Without access to the UITableViewCell instance in this method, you’re forced to use something like sizeWithFont:constrainedToSize:lineBreakMode:. But, I noticed this was not returning consistent heights for me. Or at least, not heights that seemed to make enough room for my text from the UITextView. That was the problem, though. The text view has additional left and right padding as well as different line heights, or something, than the sizeWithFont method uses.

    So, after fudging the results of the sizeWithFont method for awhile, I ended up with the following code:

    - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        // init the font that we use
        UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16];
    
        if (indexPath.section == 0) {
    
            // get the text size of the thread
            CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
    
            // text height + padding
            return textSize.height + 40;
    
        } else if (indexPath.section == 1) {
    
            // get the comment for this row
            Comment *comment = [_comments objectAtIndex:indexPath.row];
    
            // get the heights of the text areas
            CGSize textSize = [comment.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
    
            return textSize.height + 40;
    
        }
    
        return 60;
    
    }
    

    In the size constraint parameter, I subtract 16 units because the UITextView has a padding of roughly 8 units on both the left and right sides. This will give us a closer result to what we expected for the height of the text.

    I also add some padding to the final height, of 40 units, because the row itself needs some padding around the UITextView.

    There you go! Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a business case that would be perfect for multiple BackgroundWorkers. As an
This would seem to be the case in Firefox 3.5+, there I can instantiate
I would like to craft a case-insensitive regex (for JavaScript) that matches street names,
I would like to center a text element (in this case, the h2 sub-headline)
What would be the best practice for sharing localization object (in this case ResourceBundle,
What I'm trying to achieve is that a widget could exist in two different
I have the following code that I use to resize some images on my
I'm struggling a bit trying to achieve something that appeared simple initially to me.
I would like to handle the case where I don't like the format of
I would like to make a JSF inputText field into upper case when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.