如何在 iOS 6 中计算给定宽度的 NSAttributedString 的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14409897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:54:44  来源:igfitidea点击:

How to calculate the height of an NSAttributedString with given width in iOS 6

iosnsattributedstringbounding-box

提问by Jake

Possible Duplicate:
How to get height for NSAttributedString at a fixed width

可能的重复:
如何以固定宽度获取 NSAttributedString 的高度

Now NSAttributedString is available in iOS 6. For layout purposes, I want to know how to calculate the required height of an NSAttributedString under fixed width. I'm looking for something that's equivalent to NSString's - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)sizebut for NSAttributedString.

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

To calculate the drawing size of NSAttributedStrings, there are two methods available:

要计算 NSAttributedStrings 的绘图大小,有两种方法可用:

  1. - (CGSize)sizecan't be used because it does not take any width into consideration.
  2. I tried - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context, but somehow it doesn't give me the correct height. I think the method is buggy. If I run the following code, it gives me bounding size: 572.324951, 19.000000ignoring the given width of 200. It should give me something like 100 of height.
  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(@"bounding size: %f, %f", frame.size.width, frame.size.height);

There are other methods available for Mac OS X, but not for iOS.

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

回答by rmaddy

Option 2 does work in iOS with the proper parameters.

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

Without the proper values for the optionsparameter you will get the wrong height.

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

It is also required that attrStrcontains a font attribute. Without a font, there is no way to properly calculate the size.

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