使用 NSLayoutManager 获取字符串的边界矩形
我有大量独特的字符串,当它们被放置在无限大的矩形中时,我想计算它们的边界矩形。目前,我使用单个 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];
...
}
问题是:考虑到不需要绘制字符串,是否可以加快计算速度?我只对矩形的宽度和高度感兴趣。
I have a large amount of unique strings for which I want to compute their bounding rectangle when they would be laid out in an infinitly large rectangle. Currently I use a single NSTextStorage/NSLayoutManager and loop over all strings, collecting the rectangles:
// 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];
...
}
The question is: is it possible to speed up the computation considering that the strings don't need to be drawn? I'm only interested in the rectangle's width and height.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过几天的测试后我自己回答这个问题:不,不幸的是没有更快的方法使用布局管理器。使用 CoreText 似乎也快两倍左右,但 CoreText 本身也有一些令人讨厌的问题。
Jut answering this myself after a few days of testing: no, unfortunately there is no faster way using layout-managers. Using CoreText seems too be about twice as fast, but CoreText has some nasty problems itself.