5

I want to make a UITableview with dynamic height for cell. In each cell i am displaying a message of varying text. Now i want that my table cell be according to the text of the message and also my label present on cell should display the text. I am making my table cell flexible on the basis of the number of characters that can accommodate in a line. But here is the problem. Different alphabets take different space. So, i am unable to calculate how many alphabets can come in a single line. Any Suggestions to this???

rmaddy
  • 314,917
  • 42
  • 532
  • 579

5 Answers5

2

Try this :

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{CGSize size;
        if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
            // code here for iOS 5.0,6.0 and so on
            CGSize fontSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17]];
            size = fontSize;
        }
        else {
            // code here for iOS 7.0
            NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                  [UIFont fontWithName:@"Helvetica Neue" size:19], NSFontAttributeName,
                                                  nil];
            CGRect fontSizeFor7 = [text boundingRectWithSize:CGSizeMake(571, 500)
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                  attributes:attributesDictionary
                                                     context:nil];
            size = fontSizeFor7.size;

        }return size.height +30 ;

    }
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
1

You don't want to count how many characters your text has, just count the height using:

h = [NSString stringWithFormat:@"%f",
                           [post.text boundingRectWithSize:CGSizeMake(298.0f, CGFLOAT_MAX)
                           options:(NSStringDrawingUsesLineFragmentOrigin)
                           attributes:[NSDictionary dictionaryWithObject:[UIFont fontWithName:@"Helvetica Neue" size:14.0]
                           forKey: NSFontAttributeName]
                           context:nil].size.height]

Use your width for 298 and your font/fontsize.

soprof
  • 326
  • 2
  • 6
0

Check my answer

Calculate UITableViewCell height to fit string

Here you have to calculate the height of label according to the text and then set to height of the cell.

You can calculate the height and width dynamically using this.

I hope it will help you

Community
  • 1
  • 1
svrushal
  • 1,612
  • 14
  • 25
  • You are using: [ourText sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; This method has been deprecated. –  Apr 25 '14 at 11:05
  • Yes I knew that... But it work for ios version below 7.0. For other you can find the replacement of it. I have given you clue:) – svrushal Apr 25 '14 at 11:12
0
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

  NSString * yourCellStr = @"The string you want to apply to your cell at this index path";
  CGSize maxLblSize = CGSizeMake(320, 200);
  CGSize expectedLblSize = [yourCellStr sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:maxLblSize lineBreakeMode:NSLineBreakByWordWrapping];

  return expectedLblSize.height;

}
Pancho
  • 4,099
  • 1
  • 21
  • 32
-1

Please my below answer

UILabel *lbl = [[UILabel alloc] init];
 lbl.text = @"abcd";
 NSLog(@"count %lu",(unsigned long)[lbl.text length]);