那么,经过一些挖掘TextKit框架后,我终于找到了答案。
我的代码工作在这样一个循环:
while ([self needsMoreColumns]) {
[self addColumn];
}
...
- (BOOL)needsMoreColumns {
// Always create at least one column
if (self.layoutManager.textContainers.count == 0)
return YES;
// Find out the glyph range of the last column
NSRange range = [self.layoutManager glyphRangeForTextContainer:[self.layoutManager.textContainers lastObject]];
NSUInteger glyphs = [self.layoutManager numberOfGlyphs];
// Compare it with the number of glyphs
return range.location + range.length < glyphs;
}
我不包括方法[self addColumn]
,因为它是没有道理的。它只是使用我的布局的几何图形和最后一列的位置(如果有的话)来计算下一个列的CGRect
。然后,它创建各自尺寸的NSTextContainer
,并将该矩形的origin
属性存储在专用阵列中用于绘图目的。
我也发现了方法[NSLayoutManager firstUnlaidCharacterIndex]
和[NSLayoutManager firstUnlaidGlyphIndex]
但他们似乎没有按预期工作。在仅列出三列值的文本之后,它们返回整个字符串的长度,而不是第一个字符不适合第一列的位置。这就是为什么我宁愿使用基于范围的方法。
这就是所有人,安全! 皮特。
你有样品项目吗? – karthikeyan 2017-01-04 07:37:05