Alex
Alex

Reputation: 511

sizeWithFont: in iOS 7

I am getting a warning: "'sizeWithFont:constrainedToSize:lineBreakMode:' is deprecated: first deprecated in iOS 7.0" Can anyone please suggest me an alternative for this method?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Calculate height based on cell content — cell content will stretch appropriately when the height is set
Post *post = self.articleComments[indexPath.row];
CGFloat width = tableView.frame.size.width - 71 - 15;  // Width of comment text area
UIFont *commentFont = [UIFont fontWithName:@"SeroCompPro-Light" size:14];
CGFloat commentTextHeight = [post.text sizeWithFont:commentFont constrainedToSize:CGSizeMake(width, 10000) lineBreakMode:NSLineBreakByWordWrapping].height;

return commentTextHeight + 31 + 37;
}  

Upvotes: 0

Views: 2332

Answers (4)

Itesh
Itesh

Reputation: 199

This function is deprecated in ios 7. Instead of this function

sizeWithFont:constrainedToSize:lineBreakMode

use this function, Use

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context

 boundingRectWithSize:options:attributes:context:

Calculates and returns the bounding rect for the receiver drawn using the given options and display characteristics, within the specified rectangle in the current graphics context.

Parameters size The size of the rectangle to draw in.

options String drawing options.

attributes A dictionary of text attributes to be applied to the string. These are the same attributes that can be applied to an NSAttributedString object, but in the case of NSString objects, the attributes apply to the entire string, rather than ranges within the string. context

The string drawing context to use for the receiver, specifying minimum scale factor and tracking adjustments.

Upvotes: 1

DareDevil
DareDevil

Reputation: 1142

Alternate solution for supporting IOS7 and lower version-

CGSize expectedLabelSize;
if ([self respondsToSelector:@selector(sizeWithAttributes:)])
{
    expectedLabelSize = [subTitle sizeWithAttributes:@{NSFontAttributeName:subTitleLabel.font}];
}else{
    expectedLabelSize = [subTitle sizeWithFont:subTitleLabel.font constrainedToSize:subTitleLabel.frame.size lineBreakMode:NSLineBreakByWordWrapping];
}

Upvotes: 0

Mục Đồng
Mục Đồng

Reputation: 216

I used bellow code and it works:

NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@
     {
     NSFontAttributeName: font
     }];

    CGRect rect = [attributedText boundingRectWithSize:(CGSize){size.width, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];

    result = CGSizeMake(ceilf(rect.size.width), ceilf(rect.size.height));

Problem at creating a NSAttributedString and get properly width height with ceilf method

Upvotes: 0

Antonio MG
Antonio MG

Reputation: 20410

The alternative is:

- (NSSize)sizeWithAttributes:(NSDictionary *)attributes

In your case:

[string sizeWithAttributes:@{NSFontAttributeName:[UIFont fontwithName:@"SeroCompPro-Light" size:14]}];

Upvotes: 3

Related Questions