如何在iOS 6中计算给定宽度的NSAttributedString的高度[重复]

时间:2023-02-01 07:56:58

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)size but for NSAttributedString.

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

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

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

  1. - (CGSize)size can't be used because it does not take any width into consideration.
  2. - (CGSize)大小不能使用,因为它不考虑任何宽度。
  3. 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.000000 ignoring the given width of 200. It should give me something like 100 of height.
  4. 我试过 - (CGRect)boundingRectWithSize:(CGSize)大小选项:(NSStringDrawingOptions)选项上下文:(NSStringDrawingContext *)上下文,但不知怎的,它并没有给我正确的高度。我觉得这个方法很麻烦。如果我运行以下代码,它给出了我的大小: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。

1 个解决方案

#1


154  

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 options parameter you will get the wrong height.

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

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

还要求attrStr包含字体属性。没有字体,就无法正确计算尺寸。

#1


154  

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 options parameter you will get the wrong height.

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

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

还要求attrStr包含字体属性。没有字体,就无法正确计算尺寸。