sizeWithFont:constrainedToSize:不推荐使用lineBreakMode

我有以下代码:

float height = [string sizeWithFont:[UIFont systemFontOfSize:kFontSize] constrainedToSize:CGSizeMake(widthOfTextView, 999999.0f) lineBreakMode:NSLineBreakByWordWrapping].height + verticalPadding; 

但是,每当我运行我的应用程序并收到警告,告诉我这是不赞成的。 我应该使用什么以及如何将其与当前代码一起使用?

谢谢!

sizeWithFont:ConstrainedToSize:从iOS 7开始不推荐使用lineBreakMode,所以我也一直在寻找替代品的高低。 这似乎是我迄今为止找到的最佳答案:

https://stackoverflow.com/a/18746573/1275947

这被[string boundingRectWithSize:options:attributes:context]取代。 “技巧”是创建一个属性字典,其中包含您以前使用的字体和换行模式。 在你的情况下,那应该是:

 // Create a paragraph style with the desired line break mode NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; // Create the attributes dictionary with the font and paragraph style NSDictionary *attributes = @{ NSFontAttributeName:detailTextFont, NSParagraphStyleAttributeName:paragraphStyle }; // Call boundingRectWithSize:options:attributes:context for the string CGRect textRect = [string boundingRectWithSize:CGSizeMake(widthOfTextView, 999999.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil]; float height = textRect.size.height; 

如果您不使用段落样式,您将获得默认的NSLineBreakByWordWrapping。

实际上,您不需要远离最近弃用的代码。 正如它所说的,有一个据称更好的替代品,但你应该注意只有iOS7可以使用它。 市场通常要求我们至少向后定位一个主要版本…此外,一些API来来去去,您可以等待下一个主要版本,看看时间是否certificate更新代码库的好处。

它在文档中告诉你。

它表示该方法已弃用,然后告诉您应该在其位置使用的内容。

您应该使用: boundingRectWithSize:options:attributes:context: