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

use*_*ajo 145 iphone nsstring sizewithfont ios ios7

在iOS 7中,方法:

- (CGSize)sizeWithFont:(UIFont *)font
     constrainedToSize:(CGSize)size
         lineBreakMode:(NSLineBreakMode)lineBreakMode 
Run Code Online (Sandbox Code Playgroud)

和方法:

- (CGSize)sizeWithFont:(UIFont *)font
Run Code Online (Sandbox Code Playgroud)

不推荐使用.我怎样才能更换

CGSize size = [string sizeWithFont:font
                 constrainedToSize:constrainSize
                     lineBreakMode:NSLineBreakByWordWrapping];
Run Code Online (Sandbox Code Playgroud)

和:

CGSize size = [string sizeWithFont:font];
Run Code Online (Sandbox Code Playgroud)

小智 219

你可以试试这个:

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

CGSize size = textRect.size;
Run Code Online (Sandbox Code Playgroud)

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

  • `NSLineBreakByWordWrapping`将在`NSParagraphStyle`中.例如:`NSMutableParagraphStyle*paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;`在属性中,您需要添加`NSParagraphStyleAttributeName:paragraphStyle.copy` ... (30认同)
  • 什么是BOLIVIASize? (20认同)
  • 你在哪里提到NSLineBreakByWordWrapping?Wherre去了吗? (12认同)
  • 不要忘记`boundingRectWithSize:options:attributes:context:`返回小数值.你需要分别做`ceil(textRect.size.height)`和`ceil(textRect.size.width)`来获得真正的高度/宽度. (9认同)

Nir*_*rav 36

由于我们无法对大于4.3的所有iOS使用sizeWithAttributes,因此我们必须为7.0和之前的iOS编写条件代码.

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;
}
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)

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

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


Par*_*iya 9

这是简单的解决方案:

要求 :

CGSize maximumSize = CGSizeMake(widthHere, MAXFLOAT);
UIFont *font = [UIFont systemFontOfSize:sizeHere];
Run Code Online (Sandbox Code Playgroud)

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

CGSize expectedSize = [stringHere sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
Run Code Online (Sandbox Code Playgroud)

现在在更高版本的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;
Run Code Online (Sandbox Code Playgroud)


rob*_*tti 6

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

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

CGSize size = [myLabel textRectForBounds:myLabel.frame limitedToNumberOfLines:mylabel.numberOfLines].size;
Run Code Online (Sandbox Code Playgroud)

希望这可能有所帮助.

  • Apple的文档说你不应该直接调用这个方法. (5认同)

Har*_*ris 6

下面是两个简单的方法,将取代这两个弃用的方法.

以下是相关参考资料:

如果您使用的是NSLineBreakByWordWrapping,则无需指定NSParagraphStyle,因为这是默认设置:https: //developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSParagraphStyle_Class/index. HTML#// apple_ref/OCC/CLM/NSParagraphStyle/defaultParagraphStyle

您必须获得该大小的ceil,以匹配已弃用的方法的结果. 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));
}
Run Code Online (Sandbox Code Playgroud)