简体   繁体   English

在iOS 7中因过时的sizeWithFont的替换而丢失:

[英]Lost with the replacement for deprecated sizeWithFont: in iOS 7

I have a warning that sizeWithFont: is deprecated, i have try to replace it to sizeWithAttributes: but everything i try is not working. 我有一个警告,已弃用sizeWithFont:,我已尝试将其替换为sizeWithAttributes:,但我尝试的所有方法均无效。

The code is supposed to tell me the expected size of a UILabel and the cCell is the cell and the label fro the IB. 代码应该告诉我UILabel的预期大小,cCell是单元格,而IB则是标签。

Thanks for all your help. 感谢你的帮助。

CGSize maximumLabelSize = CGSizeMake(210, FLT_MAX);

expectedLabelSize = [labelText sizeWithFont:cCell.lblHotelResponse.font constrainedToSize:maximumLabelSize lineBreakMode:cCell.lblHotelResponse.lineBreakMode];

You need to use sizeWithAttributes: instead. 您需要使用sizeWithAttributes:

NSDictionary *attributeDict = @{NSFontAttributeName:cCell.lblHotelResponse.font};
CGSize expectedLabelSize    = [labelText sizeWithAttributes:attributeDict];

Another way is: 另一种方法是:

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:labelText attributes:@{NSFontAttributeName: cCell.lblHotelResponse.font}];
CGRect rect                          = [attributedString boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize labelSize                     = rect.size;

References: 参考文献:

  1. sizeWithAttributes: sizeWithAttributes:
  2. boundingRectWithSize:options:attributes: boundingRectWithSize:选项:属性:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM