简体   繁体   English

'sizeWithFont:constrainedToSize:lineBreakMode:'已弃用:

[英]'sizeWithFont:constrainedToSize:lineBreakMode:'is deprecated:

Converting a project from iOS5.0 to iOS7 / iOS6 on Xcode 5. The code below is giving a compile time warning: 在Xcode 5上将项目从iOS5.0转换为iOS7 / iOS6。下面的代码给出了编译时警告:

'sizeWithFont:constrainedToSize:lineBreakMode:'is deprecated: first deprecated in ios 7.0 - Use - boundingRectWithSize:options:attribiutes:context 'sizeWithFont:constrainedToSize:lineBreakMode:'不推荐使用:首先在ios 7.0中弃用 - 使用 - boundingRectWithSize:options:attribiutes:context

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        self.lblHidden.frame = CGRectMake(58, 228, 945, 9999);
        self.lblHidden.text = detailShareObj.pDesc;
        CGSize size = [detailShareObj.pDesc sizeWithFont:self.lblHidden.font constrainedToSize:self.lblHidden.frame.size lineBreakMode:NSLineBreakByWordWrapping];
        return 228.0+size.height+20;

    }
    else if (indexPath.section == 1)
    {
        NSString *tempPointStr = (self.shortDescArray)[indexPath.row];

        self.lblHidden.frame = CGRectMake(58, 0, 945, 9999);
        self.lblHidden.text = tempPointStr;
        CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font
                               constrainedToSize:self.lblHidden.frame.size
                                   lineBreakMode:NSLineBreakByWordWrapping];

            return 50.0f;
    }

I tried some of the suggestion give elsewhere but nothing is up to rescue if some one can help by giving the corrections required in the code will be greatly appreciated. 我尝试了其他地方给出的一些建议,但是如果有人可以通过提供代码中所需的更正来提供帮助,那么没有什么可以解决的。

I wouldn't just mask the deprecated function warning. 我不会掩盖已弃用的功能警告。 They deprecated it for a reason. 他们因为某种原因弃用了它。 I believe the function was deprecated because that series of NSString+UIKit functions were based on the UIStringDrawing library, which wasn't thread safe. 我相信该函数已被弃用,因为该系列NSString + UIKit函数基于UIStringDrawing库,该库不是线程安全的。 If you tried to run them not on the main thread (like any other UIKit functionality), you'll get unpredictable behaviors. 如果您尝试不在主线程上运行它们(就像任何其他UIKit功能一样),您将获得不可预测的行为。 In particular, if you ran the function on multiple threads simultaneously, it'll probably crash your app. 特别是,如果您同时在多个线程上运行该函数,它可能会使您的应用程序崩溃。 This is why in iOS 6, they introduced a the boundingRectWithSize:... method for NSAttributedStrings. 这就是为什么在iOS 6中,他们为NSAttributedStrings引入了boundingRectWithSize:...方法。 This was built on top of the NSStringDrawing libraries and is thread safe. 它建立在NSStringDrawing库之上,并且是线程安全的。

If you look at the new NSString boundingRectWithSize:... function, it asks for an attributes array in the same manner as a NSAttributeString. 如果查看新的NSString boundingRectWithSize:...函数,它会以与NSAttributeString相同的方式请求属性数组。 If I had to guess, this new NSString function in iOS 7 is merely a wrapper for the NSAttributeString function from iOS 6. 如果我不得不猜测,iOS 7中的这个新的NSString函数只是iOS 6中NSAttributeString函数的包装器。

On that note, if you were only supporting iOS 6 and iOS 7, then I would definitely change all of your NSString's sizeWithFont:... to the NSAttributeString's boundingRectWithSize . 在这方面,如果您只支持iOS 6和iOS 7,那么我肯定会将所有NSString的sizeWithFont:...更改为NSAttributeString的boundingRectWithSize It'll save you a lot of headache if you happen to have a weird multi-threading corner case! 如果您碰巧有一个奇怪的多线程角落案例,它会为您省去很多麻烦! Here's how I converted NSString's sizeWithFont:constrainedToSize: : 这是我如何转换NSString的sizeWithFont:constrainedToSize: ::

What used to be: 过去是什么:

NSString *text = ...;
CGFloat width = ...;
UIFont *font = ...;
CGSize size = [text sizeWithFont:font 
               constrainedToSize:(CGSize){width, CGFLOAT_MAX}];

Can be replaced with: 可以替换为:

NSString *text = ...;
CGFloat width = ...;
UIFont *font = ...;
NSAttributedString *attributedText =
    [[NSAttributedString alloc]
        initWithString:text
        attributes:@
        {
            NSFontAttributeName: font
        }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];
CGSize size = rect.size;

Please note the documentation mentions: 请注意文档提到:

In iOS 7 and later, this method returns fractional sizes (in the size component of the returned CGRect); 在iOS 7及更高版本中,此方法返回小数大小(在返回的CGRect的大小组件中); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function. 要使用返回的大小来调整视图大小,必须使用ceil函数将其值提升到最接近的更高整数。

So to pull out the calculated height or width to be used for sizing views, I would use: 因此,要拉出用于调整视图大小的计算高度或宽度,我会使用:

CGFloat height = ceilf(size.height);
CGFloat width  = ceilf(size.width);

If you want it compatible with both iOS7 and the versions below it, try this one (with ARC): 如果您希望它兼容iOS7及其下面的版本,请尝试使用此版本(使用ARC):

CGSize size;

if ([tempPointStr respondsToSelector:
     @selector(boundingRectWithSize:options:attributes:context:)])
{
  NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  paragraphStyle.alignment = NSTextAlignmentLeft;

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

  size = [tempPointStr boundingRectWithSize:self.lblHidden.frame.size
                                    options:NSStringDrawingUsesFontLeading
                                           |NSStringDrawingUsesLineFragmentOrigin
                                 attributes:attributes
                                    context:nil].size;
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  size = [tempPointStr sizeWithFont:self.lblHidden.font
                  constrainedToSize:self.lblHidden.frame.size
                      lineBreakMode:NSLineBreakByWordWrapping];
#pragma clang diagnostic pop
}

Note : It's just an example for your else-if case, maybe you need to do some modification depend on what you want it be. 注意 :这只是你的else-if例子else-if这种情况,也许你需要根据你的需要做一些修改。 ;) ;)

For iOS7, replace: 对于iOS7,请替换:

CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font
                       constrainedToSize:self.lblHidden.frame.size
                           lineBreakMode:NSLineBreakByWordWrapping];

With: 附:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; //set the line break mode
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:self.lblHidden.font, NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
CGSize size = [tempPointStr boundingRectWithSize:self.lblHidden.frame.size
                                         options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
                                      attributes:attrDict context:nil].size;

You can use: 您可以使用:

UIFont *font = [UIFont boldSystemFontOfSize:16];

CGRect new = [string boundingRectWithSize:CGSizeMake(200, 300)
    options:NSStringDrawingUsesFontLeading 
    attributes:@{NSFontAttributeName: font} 
    context:nil];

CGSize stringSize= new.size;

If you're targeting iOS 6.0+, you can still use sizeWithFont:constrainedToSize:lineBreakMode: . 如果您的目标是iOS 6.0+,您仍然可以使用sizeWithFont:constrainedToSize:lineBreakMode: . Just make sure that your project's iOS Deployment Target is set for 6.0, and the compiler won't give you these warnings. 只需确保您的项目的iOS Deployment Target设置为6.0,编译器不会给您这些警告。

(You can find this by clicking on the blue project tab (usually at the top of the left, project navigator pane) within the "info" section). (您可以通过单击“信息”部分中的蓝色项目选项卡(通常位于左侧顶部,项目导航器窗格)找到此信息)。

If you're only targeting iOS 7.0+, you should use the new method boundingRectWithSize:options:attributes:context . 如果您的目标只是iOS 7.0+,则应使用新方法boundingRectWithSize:options:attributes:context

You can find the Apple docs on this new method here. 您可以在此处找到有关此新方法的Apple文档。

The boundingRectWithSize:options:attributes:context has the problem, that it does not calculates the height correctly if the String contains "\\n" (line breaks). boundingRectWithSize:options:attributes:context有问题,如果String包含“\\ n”(换行符),它不会正确计算高度。 Therefore this code calculates the size for each line separately for a given width (inWidth): 因此,此代码分别针对给定宽度(inWidth)计算每行的大小:

NSArray *brokenByLines=[string componentsSeparatedByString:@"\n"];
CGFloat height=0.0;
CGFloat maxWidth=0.0;
for (NSString* actString in brokenByLines) {
    CGRect tSize=[actString boundingRectWithSize:CGSizeMake(inWidth, 600) options:(NSStringDrawingUsesLineFragmentOrigin | NSLineBreakByWordWrapping) attributes:@{NSFontAttributeName: inFont} context:nil];
    if (maxWidth<tSize.size.width) {
        maxWidth=tSize.size.width;
    }
    height+=tSize.size.height;
}
CGSize size= CGSizeMake(ceil(maxWidth), ceil(height));

暂无
暂无

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

相关问题 不推荐使用sizeWithFont:constrainedToSize:lineBreakMode - sizeWithFont:constrainedToSize:lineBreakMode deprecated 替换已弃用的“ sizeWithFont:constrainedToSize:lineBreakMode”的正确语法 - Correct syntax for replacement of deprecated “sizeWithFont:constrainedToSize:lineBreakMode” sizeWithFont:ConstrainedToSize:lineBreakMode:方法在iOS 7中已弃用 - sizeWithFont: ConstrainedToSize: lineBreakMode: method is deprecated in iOS 7 在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中的内存泄漏: - Memory Leak in sizeWithFont:constrainedToSize:lineBreakMode: &#39;sizeWithFont:constrainedToSize:lineBreakMode:&#39;已被弃用:在iOS 7.0中首次弃用-使用-boundingRectWithSize:options:attributes:context: - 'sizeWithFont:constrainedToSize:lineBreakMode:' is deprecated: first deprecated in iOS 7.0 - Use -boundingRectWithSize:options:attributes:context: 将sizeWithFont:constrainedToSize:lineBreakMode与NSLineBreakByTruncatingTail结合使用时的高度 - Height when using sizeWithFont:constrainedToSize:lineBreakMode with NSLineBreakByTruncatingTail sizeWithFont:constrainedToSize:lineBreakMode:使用UILineBreakModeClip时不正确? - sizeWithFont:constrainedToSize:lineBreakMode: incorrect when using UILineBreakModeClip? iOS是否对sizeWithFont:constrainedToSize:lineBreakMode中的空格有任何限制? - iOS Is there any limitation of white space number in sizeWithFont:constrainedToSize:lineBreakMode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM