IOS 7 sizeWithFont已弃用

问题描述:

我似乎无法正确地用 boundingRecWithSize 替换已弃用的 sizeWithFont 。我仔细检查了所有的答案并熬夜试图解决这个问题。我真的需要一些比我更聪明的人的帮助。这是我试图修改的代码。任何帮助将不胜感激。

I cannot seem to replace the deprecated sizeWithFont with boundingRecWithSize correctly. I scoured through all the answers and stayed up all night trying to fix this.I really need help from someone way smarter than I. Here is the code I am trying to modify. Any help would be appreciated.

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];


在apple 文档


sizeWithFont:返回字符串的大小,如果要将其渲染为
在一行上指定字体。 (在iOS 7.0中不推荐。使用
sizeWithAttributes:改为。)


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

所以你可以使用 sizeWithAttributes:像这样:

So you can use sizeWithAttributes: like this:

 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];