简体   繁体   English

不推荐使用sizeWithFont:constrainedToSize:lineBreakMode

[英]sizeWithFont:constrainedToSize:lineBreakMode deprecated

I have the following code: 我有以下代码:

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

However, whenever I run my application and get a warning telling me that this is deprecated. 但是,每当我运行我的应用程序并收到警告,告诉我这已被弃用。 What should I use and how would I use it with my current code? 我应该使用什么以及如何在当前代码中使用它?

Thanks! 谢谢!

sizeWithFont:ConstrainedToSize:lineBreakMode is deprecated as of iOS 7, so I've also been searching high and low for the replacement. 从iOS 7开始不推荐使用sizeWithFont:ConstrainedToSize:lineBreakMode,因此我一直在寻找替代品。 This seems to be the best answer I've found so far: 到目前为止,这似乎是最好的答案:

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

This is replaced by [string boundingRectWithSize:options:attributes:context]. 替换为[string boundingRectWithSize:options:attributes:context]。 The "trick" is to create an attributes dictionary that contains the font and line-break mode you were previously using. “技巧”是创建一个属性字典,其中包含您先前使用的字体和换行模式。 In your case, that should be: 在您的情况下,应为:

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

If you leave off the paragraph style, you will get the default of NSLineBreakByWordWrapping. 如果不使用段落样式,则将获得默认的NSLineBreakByWordWrapping。

You actually don't need to run away from recent deprecated code that much. 实际上,您不需要过多地使用最近不推荐使用的代码。 Its just what it says, there is a supposedly better replacement, but you should beware that only iOS7 can use it. 正是它说的那样,据说有更好的替代品,但您应该注意,只有iOS7可以使用它。 Market usually demands that we target at least one major version backwards... Also, some API come and go, you can wait for next major version to see if time proves the benefit to update your codebase. 市场通常要求我们至少将至少一个主要版本作为目标。此外,一些API来了又去,您可以等待下一个主要版本来查看时间是否证明更新代码库有好处。

It tells you in the docs . 它在docs中告诉您。

It says that the method is deprecated, and then tells you what you should be using in its place. 它说该方法已被弃用,然后告诉您应在该位置使用什么方法。

You should be using: boundingRectWithSize:options:attributes:context: 您应该使用: boundingRectWithSize:options:attributes:context:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS 7中替换弃用的-sizeWithFont:constrainedToSize:lineBreakMode: - Replacement for deprecated -sizeWithFont:constrainedToSize:lineBreakMode: in iOS 7? sizeWithFont:constrainedToSize:lineBreakMode:不准确? - sizeWithFont:constrainedToSize:lineBreakMode: not accurate? sizeWithFont:constrainedToSize:lineBreakMode:中的UILineBreakModeTailTruncation被忽略 - UILineBreakModeTailTruncation in sizeWithFont:constrainedToSize:lineBreakMode: is ignored UILabel与 - [sizeWithFont:constrainedToSize:lineBreakMode]正在切断单词 - UILabel with -[sizeWithFont:constrainedToSize:lineBreakMode] is cutting off words 为什么sizeWithFont:constrainedToSize:lineBreakMode:返回错误的大小? - Why is sizeWithFont:constrainedToSize:lineBreakMode: returning an incorrect size? 警告:“消息'sizeWithFont:constrainedToSize:lineBreakMode:'的接收者为空……” - Warning: “The receiver of message 'sizeWithFont:constrainedToSize:lineBreakMode:' is nil…” 将sizeWithFont:constrainedToSize:lineBreakMode与NSLineBreakByTruncatingTail结合使用时的高度 - Height when using sizeWithFont:constrainedToSize:lineBreakMode with NSLineBreakByTruncatingTail sizeWithFont:constrainedToSize:lineBreakMode:使用UILineBreakModeClip时不正确? - sizeWithFont:constrainedToSize:lineBreakMode: incorrect when using UILineBreakModeClip? iOS:UILabel动态高度使用sizeWithFont:constrainedToSize:lineBreakMode:不工作 - iOS: UILabel dynamic height using sizeWithFont:constrainedToSize:lineBreakMode: not working sizeWithFont:constrainedToSize:lineBreakMode:和textView.contentSize.height之间的差异 - Discrepancy between sizeWithFont:constrainedToSize:lineBreakMode: and textView.contentSize.height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM