替换已弃用的 -sizeWithFont:constrainedToSize:lineBreakMode: 在 iOS 7 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18903304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:25:16  来源:igfitidea点击:

Replacement for deprecated -sizeWithFont:constrainedToSize:lineBreakMode: in iOS 7?

iosiphoneios7nsstringsizewithfont

提问by user_Dennis_Mostajo

In iOS 7, the method:

在iOS 7中,方法:

- (CGSize)sizeWithFont:(UIFont *)font
     constrainedToSize:(CGSize)size
         lineBreakMode:(NSLineBreakMode)lineBreakMode 

and the method:

和方法:

- (CGSize)sizeWithFont:(UIFont *)font

are deprecated. How can I replace

已弃用。我该如何更换

CGSize size = [string sizeWithFont:font
                 constrainedToSize:constrainSize
                     lineBreakMode:NSLineBreakByWordWrapping];

and:

和:

CGSize size = [string sizeWithFont:font];

回答by Xavier Maro?as

You could try this:

你可以试试这个:

CGRect textRect = [text boundingRectWithSize:size
                                 options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:FONT}
                                 context:nil];

CGSize size = textRect.size;

Just change "FONT" for an "[UIFont font....]"

只需将“FONT”更改为“[UIFont font....]”

回答by Nirav

As we cant use sizeWithAttributes for all iOS greater than 4.3 we have to write conditional code for 7.0 and previous iOS.

由于我们不能对大于 4.3 的所有 iOS 使用 sizeWithAttributes,我们必须为 7.0 和以前的 iOS 编写条件代码。

1) Solution 1:

1)解决方案1:

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
   CGSize size = CGSizeMake(230,9999);
   CGRect textRect = [specialityObj.name  
       boundingRectWithSize:size
                    options:NSStringDrawingUsesLineFragmentOrigin
                 attributes:@{NSFontAttributeName:[UIFont fontWithName:[AppHandlers zHandler].fontName size:14]}
                    context:nil];
   total_height = total_height + textRect.size.height;   
}
else {
   CGSize maximumLabelSize = CGSizeMake(230,9999); 
   expectedLabelSize = [specialityObj.name sizeWithFont:[UIFont fontWithName:[AppHandlers zHandler].fontName size:14] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; //iOS 6 and previous. 
   total_height = total_height + expectedLabelSize.height;
}

2) Solution 2

2)解决方案2

UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16]; // Your Font-style whatever you want to use.
gettingSizeLabel.text = @"YOUR TEXT HERE";
gettingSizeLabel.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(310, 9999); // this width will be as per your requirement

CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];

The first solution is sometime fail to return proper value of height. so use another solution. which will work perfectly.

第一个解决方案是有时无法返回正确的高度值。所以使用另一种解决方案。这将完美地工作。

The second option is quite well and working smoothly in all iOS without conditional code.

第二个选项非常好,并且在没有条件代码的所有 iOS 中都可以顺利运行。

回答by Paresh Navadiya

Here is simple solution :

这是简单的解决方案:

Requirements :

要求 :

CGSize maximumSize = CGSizeMake(widthHere, MAXFLOAT);
UIFont *font = [UIFont systemFontOfSize:sizeHere];

Now As constrainedToSizeusage:lineBreakMode:usage is deprecated in iOS 7.0:

现在因为constrainedToSizeusage:lineBreakMode:iOS 7.0 中不推荐使用:

CGSize expectedSize = [stringHere sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];

Now usage in greaterversion of iOS 7.0will be:

现在在更高版本的iOS 7.0 中的用法将是:

// Let's make an NSAttributedString first
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:stringHere];
//Add LineBreakMode
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
[attributedString setAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, attributedString.length)];
// Add Font
[attributedString setAttributes:@{NSFontAttributeName:font} range:NSMakeRange(0, attributedString.length)];

//Now let's make the Bounding Rect
CGSize expectedSize = [attributedString boundingRectWithSize:maximumSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;

回答by Harris

Below are two simple methods that will replace these two deprecated methods.

下面是两个简单的方法,它们将取代这两个已弃用的方法。

And here are the relevant references:

以下是相关参考资料:

If you are using NSLineBreakByWordWrapping, you don't need to specify the NSParagraphStyle, as that is the default: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSParagraphStyle_Class/index.html#//apple_ref/occ/clm/NSParagraphStyle/defaultParagraphStyle

如果您使用 NSLineBreakByWordWrapping,则不需要指定 NSParagraphStyle,因为这是默认设置:https: //developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSParagraphStyle_Class/index。 html#//apple_ref/occ/clm/NSParagraphStyle/defaultParagraphStyle

You must get the ceil of the size, to match the deprecated methods' results. https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSString_UIKit_Additions/#//apple_ref/occ/instm/NSString/boundingRectWithSize:options:attributes:context:

您必须获得大小的上限,以匹配已弃用方法的结果。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSString_UIKit_Additions/#//apple_ref/occ/instm/NSString/boundingRectWithSize:options:attributes:context

+ (CGSize)text:(NSString*)text sizeWithFont:(UIFont*)font {    
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: font}];
    return CGSizeMake(ceilf(size.width), ceilf(size.height));
}

+ (CGSize)text:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size{
    CGRect textRect = [text boundingRectWithSize:size
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName: font}
                                     context:nil];
    return CGSizeMake(ceilf(textRect.size.width), ceilf(textRect.size.height));
}

回答by roberto.buratti

In most cases I used the method sizeWithFont:constrainedToSize:lineBreakMode:to estimate the minimum size for a UILabelto accomodate its text (especially when the label has to be placed inside a UITableViewCell)...

在大多数情况下,我使用方法sizeWithFont:constrainedToSize:lineBreakMode:来估计UILabel的最小大小以容纳其文本(尤其是当标签必须放置在 UITableViewCell 内时)...

...If this is exactlyyour situation you can simpy use the method:

...如果这正是您的情况,您可以简单地使用该方法:

CGSize size = [myLabel textRectForBounds:myLabel.frame limitedToNumberOfLines:mylabel.numberOfLines].size;

Hope this might help.

希望这可能会有所帮助。

回答by user3575114

UIFont *font = [UIFont boldSystemFontOfSize:16];
CGRect new = [string boundingRectWithSize:CGSizeMake(200, 300) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil];
CGSize stringSize= new.size;

回答by Dan Rosenstark

[Accepted answer works nicely in a category. I'm overwriting the deprecated method names. Is this a good idea? Seems to work with no complaints in Xcode 6.x]

[接受的答案在一个类别中效果很好。我正在覆盖已弃用的方法名称。这是一个好主意吗?在 Xcode 6.x 中似乎没有任何抱怨]

This works if your Deployment Target is 7.0 or greater. Category is NSString (Util)

如果您的部署目标是 7.0 或更高版本,则此方法有效。类别是NSString (Util)

NSString+Util.h

NSString+Util.h

- (CGSize)sizeWithFont:(UIFont *) font;
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size;

NSString+Util.m

NSString+Util.m

- (CGSize)sizeWithFont:(UIFont *) font {
    NSDictionary *fontAsAttributes = @{NSFontAttributeName:font};
    return [self sizeWithAttributes:fontAsAttributes];
}

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size {
    NSDictionary *fontAsAttributes = @{NSFontAttributeName:font};
    CGRect retVal = [self boundingRectWithSize:size
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:fontAsAttributes context:nil];
    return retVal.size;
}

回答by Alex

UIFont *font = [UIFont fontWithName:@"Courier" size:16.0f];

NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.alignment = NSTextAlignmentRight;

NSDictionary *attributes = @{ NSFontAttributeName: font,
                    NSParagraphStyleAttributeName: paragraphStyle };

CGRect textRect = [text boundingRectWithSize:size
                                 options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:attributes
                                 context:nil];

CGSize size = textRect.size;

from two answers 1+ 2

从两个答案1+ 2