Ios7 IOS 7 sizeWithFont已弃用

Ios7 IOS 7 sizeWithFont已弃用,ios7,deprecated,sizewithfont,Ios7,Deprecated,Sizewithfont,我似乎无法正确地用boundingRecWithSize替换不推荐使用的sizeWithFont。我搜遍了所有的答案,熬夜试图解决这个问题。我真的需要比我聪明的人的帮助。这是我试图修改的代码。任何帮助都将不胜感激 CGSize sizeForText = [faqItem.answer sizeWithFont:[UIFont boldSystemFontOfSize:14] constrainedToSize:CGSizeMake(self.tblView.bounds.size.wid

我似乎无法正确地用
boundingRecWithSize
替换不推荐使用的
sizeWithFont
。我搜遍了所有的答案,熬夜试图解决这个问题。我真的需要比我聪明的人的帮助。这是我试图修改的代码。任何帮助都将不胜感激

CGSize sizeForText = [faqItem.answer sizeWithFont:[UIFont boldSystemFontOfSize:14]
   constrainedToSize:CGSizeMake(self.tblView.bounds.size.width - padding, MAXFLOAT)
   lineBreakMode:NSLineBreakByWordWrapping];

[sectionInfo insertObject:[NSNumber numberWithFloat:roundf(sizeForText.height + 5)]
  inRowHeightsAtIndex:0];
在苹果:

sizeWithFont:返回要呈现的字符串的大小 在单行上使用指定的字体(在iOS 7.0中已弃用。请使用 表示敬意的尺寸:改为。)

  • (CGSize)sizeWithFont:(UIFont*)字体参数用于计算字符串大小的字体。返回值指定的宽度和高度 结果字符串的边界框。这些值可以四舍五入到 最接近的整数
所以你可以这样使用:

 CGSize sizeForText = [faqItem.answer sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]}
                       constrainedToSize:CGSizeMake(self.tblView.bounds.size.width - padding, MAXFLOAT) 
                           lineBreakMode:NSLineBreakByWordWrapping];

[sectionInfo insertObject:[NSNumber numberWithFloat:roundf(sizeForText.height + 5)] 
      inRowHeightsAtIndex:0];

您需要使用SizeWithatAttributes属性

CGSize mysize = [string sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0f]}];
您还可以将其设置为已创建的字体大小,以便在多次使用该大小时减少重新编码:

CGSize mysize = [string sizeWithAttributes:@{NSFontAttributeName: label1.font}];

我不相信您可以将constrainedToSize用于此属性。它必须在CGRect上单独设置。

我为您编写了一个示例,希望对您有所帮助

NSString *text = @"    // Do any additional setup after loading the view, typically from a nib.";
CGRect rect = CGRectZero;
NSDictionary *attrDict = @{NSFontAttributeName : [UIFont systemFontOfSize:17]};

rect = [text boundingRectWithSize:CGSizeMake(100,9999)
                          options:(NSStringDrawingUsesLineFragmentOrigin)
                       attributes:attrDict
                          context:Nil];

UILabel *lbl = [[UILabel alloc] init];
lbl.text = text;
rect.origin = CGPointMake(50, 200);
lbl.frame = rect;
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.numberOfLines = 0;
[self.view addSubview:lbl];
lbl.backgroundColor = [UIColor lightGrayColor];
可能重复的