sizeWithFont方法已被弃用。 boundingRectWithSize返回一个意外的值

在iOS7中, sizeWithFont已被弃用,所以我使用了boundingRectWithSize (它返回一个CGRect值)。 我的代码:

  UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16]; // you can use your font. CGSize maximumLabelSize = CGSizeMake(310, 9999); CGRect textRect = [myString boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:fontText} context:nil]; expectedLabelSize = CGSizeMake(textRect.size.width, textRect.size.height); 

textRect ,我得到的尺寸大于我的maximumLabelSize ,与使用sizeWithFont时的尺寸不同。 我该如何解决这个问题?

如何创build新的标签和使用sizeThatFit:(CGSize)size

 UILabel *gettingSizeLabel = [[UILabel alloc] init]; gettingSizeLabel.font = [UIFont fontWithName:@"YOUR FONT's NAME" size:16]; gettingSizeLabel.text = @"YOUR LABEL's TEXT"; gettingSizeLabel.numberOfLines = 0; gettingSizeLabel.lineBreakMode = NSLineBreakByWordWrapping; CGSize maximumLabelSize = CGSizeMake(310, CGFLOAT_MAX); CGSize expectSize = [gettingSizeLabel sizeThatFits:maximumLabelSize]; 

编辑:这个上面的代码是不适合ios 7及以上,所以请使用下面:

 CGRect textRect = [myString boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:fontText} context:nil]; 

也许你需要提供这个答案中build议的方法的附加选项:

 CGSize maximumLabelSize = CGSizeMake(310, CGFLOAT_MAX); CGRect textRect = [myString boundingRectWithSize:maximumLabelSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName: fontText} context:nil]; 

这是我的工作代码片段:

 NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:attributeDict]; NSString *headline = [dict objectForKey:@"title"]; UIFont *font = [UIFont boldSystemFontOfSize:18]; CGRect rect = [headline boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil]; CGFloat height = roundf(rect.size.height +4) 

我将4px添加到计算的高度,因为没有这些4px,有一条线丢失。

我在tableView中使用这个代码片段,并将“高度”添加到一个NSNumbers数组,我得到默认的textLabel正确的单元格高度。

如果在textLabel文本下需要更多空间,则再添加4个像素。

****更新****

我不同意“宽度错误的40px”,我喊是缺less高度的4px,因为4px是一个字母和单行的界限之间的空间的默认高度。 你可以用UILabel检查它,对于16的字体大小,你需要一个20的UILabel高度。

但是,如果你的最后一行没有“g”或其他任何东西,测量可能会错过4px的高度。

我用一个小方法重新检查了一下,我的标签的高度是20,40或60,右边的宽度小于300px。

要支持iOS6和iOS7,您可以使用我的方法:

 - (CGFloat)heightFromString:(NSString*)text withFont:(UIFont*)font constraintToWidth:(CGFloat)width { CGRect rect; float iosVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; if (iosVersion >= 7.0) { rect = [text boundingRectWithSize:CGSizeMake(width, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil]; } else { CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, 1000) lineBreakMode:NSLineBreakByWordWrapping]; rect = CGRectMake(0, 0, size.width, size.height); } NSLog(@"%@: W: %.f, H: %.f", self, rect.size.width, rect.size.height); return rect.size.height; } 

****升级****

感谢您的意见,我升级了我的function如下。 由于sizeWithFont已被弃用,并且您将在XCode中收到警告,所以我添加了诊断编译码,以删除此特定函数调用/代码块的警告。

 - (CGFloat)heightFromStringWithFont:(UIFont*)font constraintToWidth:(CGFloat)width { CGRect rect; if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) { rect = [self boundingRectWithSize:CGSizeMake(width, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil]; } else { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" CGSize size = [self sizeWithFont:font constrainedToSize:CGSizeMake(width, 1000) lineBreakMode:NSLineBreakByWordWrapping]; rect = CGRectMake(0, 0, size.width, size.height); #pragma GCC diagnostic pop } return ceil(rect.size.height); } 

除了4像素主题外:根据您使用的字体和字体权重,计算返回不同的高度值。 在我的情况下:HelveticaNeue-Medium具有16.0的字体大小,对于单行返回20.0的线高度,但是对于两行39.0,对于4行78px,对于每行都缺less1px – 从行2开始 – 但是想要为每一行你必须得到一个高度结果你的字体大小+ 4px行空间。
编码时请记住这一点!
我没有这个“问题”的function,但是当我完成的时候我会更新这个post。

如果我理解正确,你正在使用boundingRectWithSize:只是作为一种获得sizeWithFont(这意味着你要直接的CGSize,而不是CGRect)的大小的方式?

这看起来像你在找什么:

replace为不推荐sizeWithFont:在iOS 7中?

他们正在使用sizeWithAttributes:获取大小,作为sizeWithFont的替代。

你仍然使用这样的东西得到错误的大小:

 UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16]; // you can use your font. expectedLabelSize = [myString sizeWithAttributes:@{NSFontAttributeName:fontText}]; 

@ SoftDesigner的评论已经为我工作

 CGRect descriptionRect = [description boundingRectWithSize:CGSizeMake(width, 0) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} context:nil]; result = ceil(descriptionRect.size.height); 

用于查找标签运行时的大小sizewithfont在iOS 7.0中已被弃用,而不是必须使用-boundingRectWithSize:options:attributes:context: method

你可以像下面的代码一样使用它

 CGSize constraint = CGSizeMake(MAXIMUM_WIDHT, TEMP_HEIGHT); NSRange range = NSMakeRange(0, [[self.message body] length]); NSDictionary *attributes = [YOUR_LABEL.attributedText attributesAtIndex:0 effectiveRange:&range]; CGSize boundingBox = [myString boundingRectWithSize:constraint options:NSStringDrawingUsesFontLeading attributes:attributes context:nil].size; int numberOfLine = ceil((boundingBox.width) / YOUR_LABEL.frame.size.width); CGSize descSize = CGSizeMake(ceil(boundingBox.width), ceil(self.lblMessageDetail.frame.size.height*numberOfLine)); CGRect frame=YOUR_LABEL.frame; frame.size.height=descSize.height; YOUR_LABEL.frame=frame; 

在这里你必须给宽度来find高度或宽度的最大长度。

试试这是为我工作。