0

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;
}  
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
Alex
  • 511
  • 1
  • 7
  • 18
  • possible duplicate of [Replacement for deprecated sizeWithFont: in iOS 7?](http://stackoverflow.com/questions/18897896/replacement-for-deprecated-sizewithfont-in-ios-7) – Amar Sep 27 '13 at 07:31
  • Yes but that is for CGRect , i need for CGFloat . – Alex Sep 27 '13 at 07:34
  • again that is for CGRect i need for CGfloat ,CGFloat commentTextHeight = [post.text sizeWithFont:commentFont constrainedToSize:CGSizeMake(width, 10000) lineBreakMode:NSLineBreakByWordWrapping].height; – Alex Sep 27 '13 at 07:38
  • this is line where i am getting the warning ,and my output is not displaying properly . – Alex Sep 27 '13 at 07:40
  • But from CGSize u can get height.Check labelSize.height.I have used it for setting height – svrushal Sep 27 '13 at 07:42
  • What do you mean "that is for CGRect"?? There is no CGRect in that function at all. – borrrden Sep 27 '13 at 07:42
  • yeah CGSize , hey can you write the answer then , as i am new to ios. – Alex Sep 27 '13 at 07:44
  • SO isn't about always getting 100% code written for you it is about getting answers – Daij-Djan Sep 27 '13 at 07:47
  • From the labelsize, use CGFloat commentTextHeight = labelSize.height; – svrushal Sep 27 '13 at 07:47

4 Answers4

3

The alternative is:

- (NSSize)sizeWithAttributes:(NSDictionary *)attributes

In your case:

[string sizeWithAttributes:@{NSFontAttributeName:[UIFont fontwithName:@"SeroCompPro-Light" size:14]}];
Antonio MG
  • 20,382
  • 3
  • 43
  • 62
1

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Itesh
  • 199
  • 8
0

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

Mục Đồng
  • 216
  • 1
  • 12
0

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];
}
DareDevil
  • 902
  • 8
  • 21