【问题标题】:Lost with the replacement for deprecated sizeWithFont: in iOS 7替换已弃用的 sizeWithFont: 在 iOS 7 中丢失
【发布时间】:2015-03-06 09:09:20
【问题描述】:

我收到 sizeWithFont: 已弃用的警告,我已尝试将其替换为 sizeWithAttributes: 但我尝试的一切都不起作用。

代码应该告诉我 UILabel 的预期大小,而 cCell 是单元格和来自 IB 的标签。

感谢您的所有帮助。

CGSize maximumLabelSize = CGSizeMake(210, FLT_MAX);

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

【问题讨论】:

标签: ios xcode cocoa-touch ios7


【解决方案1】:

您需要改用sizeWithAttributes:

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

另一种方式是:

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

参考资料:

  1. sizeWithAttributes:
  2. boundingRectWithSize:options:attributes:

【讨论】:

    猜你喜欢
    • 2013-09-24
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多