【问题标题】:How to calculate the height of an NSAttributedString with given width in iOS 6 [duplicate]如何在iOS 6中计算具有给定宽度的NSAttributedString的高度[重复]
【发布时间】:2013-01-19 00:39:51
【问题描述】:

可能重复:
How to get height for NSAttributedString at a fixed width

现在 NSAttributedString 在 iOS 6 中可用。出于布局目的,我想知道如何计算 NSAttributedString 在固定宽度下所需的高度。我正在寻找与 NSString 的 - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size 等效的东西,但对于 NSAttributedString。

计算 NSAttributedStrings 的绘制大小,有两种方法:

  1. - (CGSize)size 不能使用,因为它没有考虑任何宽度。
  2. 我试过- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context,但不知怎的,它并没有给我正确的高度。我认为该方法是错误的。如果我运行以下代码,它会给我bounding size: 572.324951, 19.000000,忽略给定的 200 宽度。它应该给我 100 的高度。
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init]; NSDictionary *attributes = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:15], NSForegroundColorAttributeName : [UIColor blueColor]}; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]]; CGRect frame = [attributedString boundingRectWithSize:CGSizeMake(200, 1000) options:0 context:nil]; NSLog(@"边界大小: %f, %f", frame.size.width, frame.size.height);

还有其他方法可用于 Mac OS X,但不适用于 iOS。

【问题讨论】:

  • @SimonGoldeen 这不是一个好的复制品。接受的答案实际上并没有给出高度,其他答案也没有显示使用 boundingRectWithSize:options:context: 方法的正确方法。
  • 在 Swift 中,这可以通过 let desiredWidth: CGFloat = 300; let rect = attrStr.boundingRect(with: CGSize(width: desiredWidth, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil) 完成

标签: ios nsattributedstring bounding-box


【解决方案1】:

选项 2 可以在 iOS 中使用适当的参数。

NSAttributedString *attrStr = ... // your attributed string
CGFloat width = 300; // whatever your desired width is
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];

如果options 参数没有正确的值,您将得到错误的高度。

attrStr 还需要包含字体属性。没有字体,就无法正确计算大小。

【讨论】:

  • 嗯...不适合我。我似乎只有一半的时间能得到很好的计算。
  • 最后一种计算NSAttributedString大小的简单而实用的方法,即使在非常复杂的格式下也能工作。类似问题的其他答案甚至可以在 NSAttributedString 上创建一个类别 - 幸运的是它不必那么难。
  • @Dogweather 根据我的经验,NSAttributedString 必须有一个 NSFontAttributeName 键才能正确调整大小。
  • @bentford 谢谢你的提示 - 我正在使用 -initWithString: 方法创建一个属性字符串,这导致了完全任意的计算大小。使用 -initWithString:attributes: 有效!
  • 我们可能希望使用CGFLOAT_MAX 作为尺寸参数,而不是10000,在某个时间点,它实际上可能适合我们的屏幕...
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 2013-10-03
  • 1970-01-01
  • 2017-11-03
相关资源
最近更新 更多