Ios 获取NSAttributed字符串的大小

Ios 获取NSAttributed字符串的大小,ios,objective-c,nsattributedstring,Ios,Objective C,Nsattributedstring,我一直在尝试获取NSAttribute字符串的大小,如下所示: CGSize myPaperSize = CGSizeMake(myPaperWidth, CGFLOAT_MAX); UIFont *font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f]; //TODO: Fix problem with printing. When we print it's overlapping because of how the printe

我一直在尝试获取NSAttribute字符串的大小,如下所示:

CGSize myPaperSize = CGSizeMake(myPaperWidth, CGFLOAT_MAX);

UIFont *font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];

//TODO: Fix problem with printing. When we print it's overlapping because of how the printer gets its size

/**

    We currently want to do it by the size of the text. But for some reason it only does it well for two lines. Over two lines overlaps
 */

NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByWordWrapping;

//Attributes of my text
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:newFormatter.text attributes:@{NSFontAttributeName: font, NSParagraphStyleAttributeName: style}];

CGRect stringRect = [attributedString boundingRectWithSize:paperRect options: NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize stringSize = CGSizeMake(stringRect.size.width, stringRect.size.height);


//Here I want to update the size of the paper by adding the distance from last formatter plus whatever the height of the string is.
self.slipHeight += distanceFromFormatterAbove + ceil(stringSize.height);

当文本在我的一张纸上有两行时,这就行了。但是当文本超过3行时,它就不起作用了。有人知道如何让它工作吗(ios7)?如果您有任何问题,请告诉我。

尝试添加NSStringDrawingUserFontLeading,以在BoundRectrictWithSize中显示您的选项

CGRect stringRect = [attributedString boundingRectWithSize:paperRect options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];

不幸的是,这没有起作用。我注意到有一件事可能是错误的,那就是在我的打印纸上,我的文本中心对齐了。我应该在我的属性上也指定它吗?我想这可能会给我错误的值。boundingRectWithSize返回的大小是多少?它的宽度和你期望的一样吗?