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 8836189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:26:09+00:00 2026-06-14T09:26:09+00:00

I have a table view where my cells height is defined dynamically depending on

  • 0

I have a table view where my cells height is defined dynamically depending on the text it is representing.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //getting my text for this cell, the row etc ...
    ...
    //here is the part interesting us
    NSAttributedString* theText = [myTextForThisCell objectAtIndex:indexPath.row];

    NSInteger labelWidth = self.tableView.bounds.size.width-HORIZONTAL_CELL_PADDING;

    CGSize textSize = [theText sizeWithFont:[UIFont systemFontOfSize:customFontSize] constrainedToSize:CGSizeMake(labelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

    return textSize.height+VERTICAL_CELL_PADDING;
}

Ok so now my problem.
The tableview is the result of a search action which after scanning a plist file shows the lines containing a given string.

Up to now that was it. But now with iOS 6 and NSAttributedString allowing easily to bold part of strings I decided to bold the search word.

It is working, it bolds the words I want but now I am no more able to calculate the cell height as sizeWithFont ask for a NSString. And as bolding takes a wider width I cannot simply calculate the cell height with the string without attributes.

I am simply stuck here.

Anyone can help me ?

  • 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-14T09:26:10+00:00Added an answer on June 14, 2026 at 9:26 am

    In fact I simply had to read apple documentation of NSAttributedText.

    In my case I have to replace the last two lines of code by

    CGRect rectSize = [theText boundingRectWithSize:CGSizeMake(labelWidth, MAXFLOAT) 
                             options:NSStringDrawingUsesLineFragmentOrigin context:NULL];
    
    return rectSize.size.height+VERTICAL_CELL_PADDING;
    

    FOLLOW UP iOS 7

    I have been struggling making this work in iOS7 with attributed text.

    Apple documentation says

    In iOS 7 and later, this method returns fractional sizes (in the size
    component of the returned CGRect); to use a returned size to size
    views, you must use raise its value to the nearest higher integer
    using the ceil function.

    Which way obviously not working for me ! For me the solution was to add +1 to the ceil of height. This is probably a bug from Apple, but for me now everything works as in iOS6.

    CGRect rectSize = [theAttributedText boundingRectWithSize:CGSizeMake(labelWidth, MAXFLOAT) 
                              options:NSStringDrawingUsesLineFragmentOrigin context:NULL];
    
    return ceil(rectSize.size.height) + 1 + VERTICAL_CELL_PADDING;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Table View Controller with cells. I want to update the text
I have a table view where the cells have a variable height. This causes
I'm using custom cells in my table view. I have the height of the
In my application I have table view cells with height of table view. When
I have a table view with numerous cells. When I press one, a tick
I have my table view and the cells within it have the UILongPressGestureRecognizer added
I have a table view controller with custom cells. In those cells i added
Using storyboard, I have a table view controller containing multiple dynamic prototype cells. One
I have a UITableView with cells containing variable-height UILabels. I am able to calculate
I have a UITableView that is populated with cells of a variable height. I

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.