获得NSAttributedString的大小

时间:2014-05-02 20:51:23

标签: ios objective-c nsattributedstring

我一直试图像这样获得NSAttributedString的大小:

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)?如果您有任何问题,请告诉我。

1 个答案:

答案 0 :(得分:0)

尝试将NSStringDrawingUsesFontLeading添加到boundRectWithSize中的选项

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