Question

I know that there are many potential answers to this question on here already but 99.995% of all the answers so far that I have seen use sizeWithFont, which is now deprecated.

I have one tableviewcell that I am dealing with here so it should be simple but things are not working out for me. here is the code that has been put together by reading some answers online. This is for a cell label.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSString *myString = [self.locations[indexPath.row] locationDescription];
  return [self heightForText:myString] + 44.0;
}

-(CGFloat)heightForText:(NSString *)text{

  NSInteger MAX_HEIGHT = 10000;
  UILabel *cellLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, MAX_HEIGHT)];
  cellLabelView.text = text;
  cellLabelView.font = [UIFont fontWithName:@"Helvetica Neue Light" size:18];
  [cellLabelView sizeToFit];
  return cellLabelView.frame.size.height;
}

The height defined in the storyboard is set to 44 and the lines is set to 0 for the label itself so that the label can decide how big it needs to be.

Problem #1: If the text that is set to the label doesn't word wrap I still (obviously) get the extra padding which is not what i want. I am not sure how to calculate if there was actually a word wrap or not. If there wasnt a word wrap (i.e the text wasnt longer than the label, then i just want to return 44).

Problem #2: For some reason if my text is too long, like 5 lines worth as an example, some of the text at the end of the string gets cut off for some reason, despite the MAXHEIGHT being 10,000, its almost as if it decides to stop word wrapping. If i increase the padding at this point, to lets say 88 then i can see everything. (Weird).

Looking for some elegant solutions, feedback, help, whatever. THanks!

No correct solution

OTHER TIPS

NSString's sizeWithFont: has indeed been deprecated in iOS 7, but replaced by sizeWithAttributes:. For multiple-line text, boundingRectWithSize:options:attributes:context: is useful. See

iOS 7 sizeWithAttributes: replacement for sizeWithFont:constrainedToSize

for a relevant alternative to your UILabel method.

Problem #1: I'm not sure exactly what your desired effect is here. If you're looking to have a constant padding above and below your UILabel, but with a minimum cell height of 44 points, you might want to do something like:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSString *myString = [self.locations[indexPath.row] locationDescription];
  CGFloat myPadding = 20.0; // for example
  return MAX([self heightForText:myString] + myPadding, 44.0);
}

However, if you'd like to base your cell height on the number of lines of text somehow, then dividing the calculated height by the UIFont's lineHeight property and rounding will work:

NSUInteger numberOfLines = roundf(calculatedTextHeight / font.lineHeight);

Problem #2: The label you're creating to calculate the height hasn't had its numberOfLines set to 0, so won't be sizing itself for a multiple-line label.

I think the NSString method is the best route to go down, but as an aside, if you'd like to use the UILabel approach, I'd recommend creating the label only once to help the UITableView scrolling performance e.g.

-(CGFloat)heightForText:(NSString *)text{
  NSInteger MAX_HEIGHT = 10000;
  static UILabel *cellLabelView = nil;
  if (!cellLabelView)
  {
     cellLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, MAX_HEIGHT)];
     cellLabelView.text = text;
     cellLabelView.font = [UIFont fontWithName:@"Helvetica Neue Light" size:18]; 
     cellLabelView.numberOfLines = 0;     
  } 
  return [cellLabelView sizeThatFits:CGSizeMake(320, MAX_HEIGHT)].height;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top