0

My code is

CGSize textSize =  [text sizeWithFont:font constrainedToSize:CGSizeMake(self.bounds.size.width - 106, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

Anyone knows how can i fix this warning?

Utku Dalmaz
  • 9,780
  • 28
  • 90
  • 130
  • 2
    may be this works `NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; CGRect textRect = [strText boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName: paragraphStyle.copy} context:nil];` – Bhavin Bhadani Apr 27 '16 at 13:03
  • follow this link http://stackoverflow.com/questions/18897896/replacement-for-deprecated-sizewithfont-in-ios-7 – Sheereen S Apr 27 '16 at 13:05
  • I am not familiar with objective-c, i have this warning in a library that i use in swift project. Trying to fix – Utku Dalmaz Apr 27 '16 at 13:07
  • If you not familiar with ObjectiveC code. Use https://objectivec2swift.com/#/converter/code. It will convert ObjC to swift – Sheereen S Apr 27 '16 at 13:11

3 Answers3

12

Swift

 // adjust the label height (top align text)
// old
var labelSize: CGSize = model.name.sizeWithFont(self.nameLabel.font,  constrainedToSize: maxNameLabelSize, lineBreakMode: self.nameLabel.lineBreakMode)
// new
var labelSize: CGSize = model.name.boundingRectWithSize(maxNameLabelSize, options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: self.nameLabel.font], context: nil).size

Objective c

 // adjust the label height (top align text)
 // old
CGSize labelSize = [model.name sizeWithFont:self.nameLabel.font
                      constrainedToSize:_maxNameLabelSize
                          lineBreakMode:self.nameLabel.lineBreakMode];
 // new
CGSize labelSize = [model.name boundingRectWithSize:_maxNameLabelSize
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:@{NSFontAttributeName: self.nameLabel.font}
                                        context:nil].size;
Sheereen S
  • 1,292
  • 10
  • 18
1

Try below code:

 NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:@"This is long text..!!!" attributes:@{NSFontAttributeName: self.lbl.font
 }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){self.lbl.frame.size.width, CGFLOAT_MAX}
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];

NSLog(@"Height %f",rect.size.height);
NSLog(@"Width %f",rect.size.width);
iVarun
  • 6,496
  • 2
  • 26
  • 34
0

Use this:

CGRect textRect = [text boundingRectWithSize:CGSizeMake(self.bounds.size.width - 106, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:[[UIFont fontWithName:@"YOUR_FONT_NAME" size:12.0f]]}
                                 context:nil];

CGSize size = textRect.size;
Soumalya Banerjee
  • 1,966
  • 3
  • 22
  • 28