简体   繁体   English

IOS 7 sizeWithFont已弃用

[英]IOS 7 sizeWithFont Deprecated

I cannot seem to replace the deprecated sizeWithFont with boundingRecWithSize correctly. 我似乎无法正确地用boundingRecWithSize替换已弃用的sizeWithFont I scoured through all the answers and stayed up all night trying to fix this.I really need help from someone way smarter than I. Here is the code I am trying to modify. 我仔细检查了所有的答案,并熬夜试图解决这个问题。我真的需要一些比我聪明的人的帮助。这是我试图修改的代码。 Any help would be appreciated. 任何帮助,将不胜感激。

CGSize sizeForText = [faqItem.answer sizeWithFont:[UIFont boldSystemFontOfSize:14]
   constrainedToSize:CGSizeMake(self.tblView.bounds.size.width - padding, MAXFLOAT)
   lineBreakMode:NSLineBreakByWordWrapping];

[sectionInfo insertObject:[NSNumber numberWithFloat:roundf(sizeForText.height + 5)]
  inRowHeightsAtIndex:0];

You need to use the sizeWithAttributes property. 您需要使用sizeWithAttributes属性。

CGSize mysize = [string sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0f]}];

You can also set it to an already created font size in order to reduce recoding if you use the size more than once: 您还可以将其设置为已创建的字体大小,以便在多次使用大小时减少重新编码:

CGSize mysize = [string sizeWithAttributes:@{NSFontAttributeName: label1.font}];

I do not believe you can use constrainedToSize with this property. 我不相信你可以使用这个属性的constrainedToSize。 It would have to be separately set on a CGRect. 它必须在CGRect上单独设置。

I wrote an sample for you, hope it's helpful. 我为你写了一个样本,希望它有用。

NSString *text = @"    // Do any additional setup after loading the view, typically from a nib.";
CGRect rect = CGRectZero;
NSDictionary *attrDict = @{NSFontAttributeName : [UIFont systemFontOfSize:17]};

rect = [text boundingRectWithSize:CGSizeMake(100,9999)
                          options:(NSStringDrawingUsesLineFragmentOrigin)
                       attributes:attrDict
                          context:Nil];

UILabel *lbl = [[UILabel alloc] init];
lbl.text = text;
rect.origin = CGPointMake(50, 200);
lbl.frame = rect;
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.numberOfLines = 0;
[self.view addSubview:lbl];
lbl.backgroundColor = [UIColor lightGrayColor];

In apple documentation : 在苹果文档中

sizeWithFont: Returns the size of the string if it were to be rendered with the specified font on a single line. sizeWithFont:如果要在单行上使用指定的字体进行渲染,则返回字符串的大小。 (Deprecated in iOS 7.0. Use sizeWithAttributes: instead.) (在iOS 7.0中不推荐使用。请改用sizeWithAttributes:)

  • (CGSize)sizeWithFont:(UIFont *)font Parameters font The font to use for computing the string size. (CGSize)sizeWithFont:(UIFont *)font参数font用于计算字符串大小的字体。 Return Value The width and height of the resulting string's bounding box. 返回值结果字符串的边界框的宽度和高度。 These values may be rounded up to the nearest whole number. 这些值可以四舍五入到最接近的整数。

So you can use sizeWithAttributes: like this: 所以你可以使用sizeWithAttributes:像这样:

 CGSize sizeForText = [faqItem.answer sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]}
                       constrainedToSize:CGSizeMake(self.tblView.bounds.size.width - padding, MAXFLOAT) 
                           lineBreakMode:NSLineBreakByWordWrapping];

[sectionInfo insertObject:[NSNumber numberWithFloat:roundf(sizeForText.height + 5)] 
      inRowHeightsAtIndex:0];

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

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