那麼,經過一些挖掘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