8

I am tring to calculate the height of text labels in uitableview cells. After seeing that sizewithfont was deprecated with ios 7, I implemented sizewithattributes but the return values were way smaller than what it should be for the label to be of correct size for the text it contains. I also tried the sizetofit method also to no avail.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSDictionary *message =  self.messages[indexPath.row];

    UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:1];
    UILabel *messageContent = (UILabel *)[cell.contentView viewWithTag:3];
    UIImageView *image = (UIImageView *)[cell.contentView viewWithTag:2];
    messageContent.text = [message objectForKey:@"messageContent"];
    NSString *content = [message objectForKey:@"messageContent"];
    NSLog(@"Message: %@", content);

    CGSize textSize = [content sizeWithAttributes:@{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]}];
    messageContent.font = [UIFont fontWithName:@"HelveticaNue-Light" size:17.0];
    CGRect messageFrame = messageContent.frame;
    messageFrame.size = textSize;
    messageContent.frame = messageFrame;


    nameLabel.text = [message objectForKey:@"senderName"];
    NSString *senderPicture = [message objectForKey:@"senderPicture"];
    UIImage* myImage = [UIImage imageWithData:
                    [NSData dataWithContentsOfURL:
                     [NSURL URLWithString: senderPicture]]];

    image.image = myImage;
    image.layer.cornerRadius = 27.0;
    image.layer.masksToBounds = YES;

    //Configure the cell...

    return cell;
  }
Pradhyuman sinh
  • 3,938
  • 1
  • 19
  • 38
user2489946
  • 271
  • 3
  • 12

1 Answers1

9

Attribution:

user/Elio.d has a great answer here.

I've attached a transcript of his answer below. If it helps you please be sure to go send Elio.d an upvote on his original answer


Transcript:

well you can try this :

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0f]};
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
CGRect rect = [textToMeasure boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:attributes
                                          context:nil];
Community
  • 1
  • 1
Pradhyuman sinh
  • 3,938
  • 1
  • 19
  • 38
  • This works, but only when i set autolayout to off in IB and when i do the labels are all out of place in the uitableview cells i have them in – user2489946 Nov 28 '13 at 19:08
  • 5
    This answer is from elio.d, not yours. http://stackoverflow.com/questions/19145078/ios-7-sizewithattributes-replacement-for-sizewithfontconstrainedtosize – Brave Oct 04 '15 at 06:46
  • 1
    @Brave copy pasting other people's work like that is so lame. I've edited in a proper attribution for him, hopefully it will drive more upvotes to Elio.d – Albert Renshaw Oct 04 '16 at 23:27