sizeWithFont:constrainedToSize - iOS7

312 Views Asked by At

I have the following method that I used in iOS6 but with iOS7 I'm getting errors on

CGSize labelHeight = [tweetText sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(self.tweetsTableView.bounds.size.width - 84, 4000)];

full method below, any ideas on how to amend for iOS7?

- (CGFloat)heightForCellAtIndex:(NSUInteger)index {

    NSDictionary *tweet = self.tweets[index];
    CGFloat cellHeight = 50;
    NSString *tweetText = tweet[@"text"];

    CGSize labelHeight = [tweetText sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(self.tweetsTableView.bounds.size.width - 84, 4000)];

    cellHeight += labelHeight.height;
    return cellHeight;
}
2

There are 2 best solutions below

0
On

I know this is an old question & late answer, but it's still very relevant,

This sizeWithFont method is now deprecated, this new method works best

NSString *content = **Whatever your label's content is expected to be**
CGSize maximumLabelSize = CGSizeMake(390, 1000);

NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey: NSFontAttributeName];

CGSize newExpectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;

So you can adjust your label (or table cell etc) to

label.frame.size.height = newExpectedLabelSize.height;

I hope this helps, cheers, Jim.

0
On

Add this lines:

UIFont *font = [UIFont boldSystemFontOfSize:16];
CGRect new = [string boundingRectWithSize:CGSizeMake(200, 300) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil];
CGSize stringSize= new.size;