当前位置: 首页 > 知识库问答 >
问题:

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

童子明
2023-03-14

在iOS7中,该方法:

- (CGSize)sizeWithFont:(UIFont *)font
     constrainedToSize:(CGSize)size
         lineBreakMode:(NSLineBreakMode)lineBreakMode 

和方法:

- (CGSize)sizeWithFont:(UIFont *)font

已弃用。如何更换

CGSize size = [string sizeWithFont:font
                 constrainedToSize:constrainSize
                     lineBreakMode:NSLineBreakByWordWrapping];

和:

CGSize size = [string sizeWithFont:font];

共有3个答案

潘阳舒
2023-03-14

这是简单的解决方案:

要求:

CGSize maximumSize = CGSizeMake(widthHere, MAXFLOAT);
UIFont *font = [UIFont systemFontOfSize:sizeHere];

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

CGSize expectedSize = [stringHere sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];

现在,在更大版本的iOS7.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;
鲜于宏义
2023-03-14

由于我们不能为所有大于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;
}

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];

第一种解决方案是有时无法返回适当的高度值。因此,请使用另一种解决方案。这将完美地工作。

第二个选项非常好,在没有条件代码的所有iOS中都能顺利工作。

羊舌承天
2023-03-14

你可以试试这个:

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

CGSize size = textRect.size;

只需将“字体”更改为“[UIFont字体....]"

 类似资料:
  • 似乎是在SpringLDAP 2。x、 OdmManager工具已被弃用,因为大多数类似odm的事情都可以由ldapTemplate完成,这是事实。但是OdmManager能够注入一个ConverterManager,它可以告诉您自定义类型转换的情况。对于类odm(ConverterManager)操作,使用ldapTemplate的等效方法是什么? 如果ldapTemplate中没有等效系统,应

  • 我对有问题: 应用程序启动时会显示警告: 警告7388---[main]组织。冬眠orm。弃用:HH90000014:发现使用弃用的[org.hibernate.id.SequenceHiLoGenerator]基于序列的id生成器;使用组织。冬眠id.enhanced。相反,SequenceStyleGenerator。有关详细信息,请参阅《Hibernate域模型映射指南》 我试图找出如何用新

  • 问题内容: 需要向订单项添加自定义元。用Google搜索它,大多数文章说使用“ woocommerce_add_order_item_meta”钩子。最新版本2.3.7不推荐使用此挂钩。有人,请告诉我使用哪个挂钩。 http://docs.woothemes.com/wc-apidocs/function- woocommerce_add_order_item_meta.html 问题答案: 如果

  • 我刚开始编译iOS 11,注意到苹果现在宣布了这个属性 由于不赞成: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621372-automaticallyadjustsscrollviewin iOS 11中是否有其他属性修复此警告? 默认值会保持为真吗?或者将来会如何处理?

  • 在Spring 3.2中,JdbcTemplate中的queryforInt/queryforLong方法被弃用。我不知道为什么或者什么是使用这些方法替换现有代码的最佳实践。 典型方法: OK以上方法需要重写如下: 显然,这种不赞成使JdbcTemplate类更简单(或者是这样吗?)。QueryForInt一直是一种方便的方法(我想),而且已经存在很长时间了。为什么要删除它。因此,代码变得更加复杂

  • 我正在将java应用程序从SpringBoot1.5迁移到SpringBoot2.2.3,这涉及到将Hibernate/Envers升级到5.4.10。最终的 现有代码调用AuditReader#getCurrentRevision(类revisionEntityClass,boolean persist),该类已被弃用,JavaDocs状态为使用RevisionListener,但这与我们需要获