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!