问题描述
现在,iOS 6中提供了NSAttributedString。出于布局目的,我想知道如何在固定宽度下计算NSAttributedString所需的高度。我正在寻找的东西相当于NSString的 - (CGSize)sizeWithFont :( UIFont *)font constrainedToSize:(CGSize)size
但是对于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.
要计算NSAttributedStrings的绘图大小,有两种方法可用:
To calculate the drawing size of NSAttributedStrings, there are two methods available:
-
- (CGSize)size
无法使用,因为它不考虑任何宽度。 - 我试过
- (CGRect )boundingRectWithSize:(CGSize)大小选项:(NSStringDrawingOptions)选项上下文:(NSStringDrawingContext *)context
,但不知何故它不能给我正确的高度。我觉得这个方法很麻烦。如果我运行以下代码,它会给我边界大小:572.324951,19.000000
忽略给定宽度200.它应该给我100个高度。
- (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.
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);
Mac OS X还有其他方法,但不适用于iOS。
There are other methods available for Mac OS X, but not for iOS.
推荐答案
选项2在iOS中使用适当的参数。
Option 2 does work in iOS with the proper parameters.
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.
还需要 attrStr
包含字体属性。没有字体,就无法正确计算尺寸。
It is also required that attrStr
contains a font attribute. Without a font, there is no way to properly calculate the size.
这篇关于如何在iOS 6中计算给定宽度的NSAttributedString的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!