NSAttributedString多行给出错误的高度?

时间:2013-07-23 16:17:54

标签: ios ipad ios6 nsattributedstring

我想计算NSAttributedString Multiline的高度。单线工作正常(意味着从高处减去前导)。但是如果文本是多行的,那么它就像单行一样。

NSMutableParagraphStyle *titleParagraphStyle = [[NSMutableParagraphStyle alloc] init];
titleParagraphStyle.lineHeightMultiple = 0.9f;
titleParagraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

CGSize constrainedSize = CGSizeMake(TOUNameWidth, MAXFLOAT); 

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      TOUNameFont, NSFontAttributeName,
                                      titleParagraphStyle, NSParagraphStyleAttributeName,
                                      nil];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString: name attributes:attributesDictionary];


CGRect rect = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesFontLeading context:nil];

NSLog(@"rect is %f",rect.size.height);

1 个答案:

答案 0 :(得分:4)

根据boundingRectWithSize:options:context:的文档,您需要添加NSStringDrawingUsesLineFragmentOrigin

  

如果未指定NSStringDrawingUsesLineFragmentOrigin,则   矩形的高度将被忽略,操作被认为是   单行渲染。 (由于存在错误,iOS上的宽度也会被忽略   6。)