我有大量独特的字符串,当它们被布置在一个无限大的矩形中时,我想计算它们的边界矩形。目前我使用单个 NSTextStorage/NSLayoutManager 并遍历所有字符串,收集矩形:
// setup NSTextStorage and its NSLayoutManager, NSTextContainer
...
forall (NSAttributedString *astring in ...)
{
// put string into textstorage
[textStorage setAttributedString:astring];
// trigger glyph generation and layout
[textContainer setContainerSize: NSMakeSize (CGFLOAT_MAX, CGFLOAT_MAX)];
[layoutManager ensureLayoutForTextContainer: textContainer];
// finally get the bounding box
NSRect boundingBox = [layoutManager usedRectForTextContainer: textContainer];
...
}
问题是:考虑到不需要绘制字符串,是否可以加快计算速度?我只对矩形的宽度和高度感兴趣。