0

The following NSString methods have been deprecated in iOS 7:

    - (CGSize)sizeWithFont:(UIFont *)font NS_DEPRECATED_IOS(2_0, 7_0, "Use -sizeWithAttributes:");

    - (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:
(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:");

    - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:");

    - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:"); 

While the replacement for the first one is straightforward, the rest not so much. What are the equivalent calls to boundingRectWithSize:options:attributes:context: to obtain exactly the same result?

In particular:

  • What should be the value of options?
  • How do you specify the lineBreakMode?
hpique
  • 119,096
  • 131
  • 338
  • 476
  • I have a feeling that this issue is about to fuel SO for the next year – Stavash Sep 30 '13 at 10:22
  • It should be noted that this question contains most of the answer to mine (even if the scope was different): http://stackoverflow.com/questions/18897896/replacement-for-deprecated-sizewithfont-in-ios-7?rq=1. Maybe a more detailed answer to the other question would be more appropriate. I'm voting to close my own question just in case. – hpique Sep 30 '13 at 10:27

2 Answers2

1

.How do you specify the lineBreakMode?

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[attributedStr addAttribute:NSParagraphStyleAttributeName
                 value:paragraphStyle
                 range:NSMakeRange(0,[attributedStr length])];
python
  • 1,407
  • 1
  • 15
  • 31
1

Add this

UIFont *font = [UIFont boldSystemFontOfSize:16];
CGRect new = [string boundingRectWithSize:CGSizeMake(200, 300) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil];
CGSize stringSize= new.size;
JuJoDi
  • 14,627
  • 23
  • 80
  • 126
user3575114
  • 993
  • 7
  • 13