如何在 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
How to calculate the height of an NSAttributedString with given width in iOS 6
提问by Jake
Possible Duplicate:
How to get height for NSAttributedString at a fixed width
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)size
但用于 NSAttributedString 的东西。
To calculate the drawing size of NSAttributedStrings, there are two methods available:
要计算 NSAttributedStrings 的绘图大小,有两种方法可用:
- (CGSize)size
can't be used because it does not take any width into consideration.- 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 mebounding size: 572.324951, 19.000000
ignoring the given width of 200. It should give me something like 100 of height.
- (CGSize)size
不能使用,因为它没有考虑任何宽度。- 我试过了
- (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 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
包含字体属性。没有字体,就无法正确计算大小。