這裏有兩個元件:
一個很好的解釋/樣品的多列與IOS 7文本試劑盒:
https://github.com/ShinobiControls/iOS7-day-by-day/blob/master/21-multi-column-textkit/21-multi-column-textkit.md
基於該樣品和以下問題:
How do I locate the CGRect for a substring of text in a UILabel?
你可以嘗試這樣的事情:
- (void)layoutTextContainers
{
NSUInteger lastRenderedGlyph = 0;
CGFloat currentXOffset = 0;
while (lastRenderedGlyph < _layoutManager.numberOfGlyphs) {
CGRect textViewFrame = CGRectMake(currentXOffset, 10,
CGRectGetWidth(self.view.bounds)/2,
CGRectGetHeight(self.view.bounds) - 20);
CGSize columnSize = CGSizeMake(CGRectGetWidth(textViewFrame) - 20,
CGRectGetHeight(textViewFrame) - 10);
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize];
[_layoutManager addTextContainer:textContainer];
// And a text view to render it
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
textContainer:textContainer];
textView.scrollEnabled = NO;
[self.scrollView addSubview:textView];
// Increase the current offset
currentXOffset += CGRectGetWidth(textViewFrame);
// And find the index of the glyph we've just rendered
lastRenderedGlyph = NSMaxRange([_layoutManager glyphRangeForTextContainer:textContainer]);
NSLog(@"Last rendered glyph %i",lastRenderedGlyph);
NSLog(@"Textview length %i",textView.text.length);
NSRange range = {lastRenderedGlyph-1, lastRenderedGlyph};
NSRange glyphRange;
// Convert the range for glyphs.
[_layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange];
NSLog(@"Glyph rect %@",NSStringFromCGRect([_layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer]));
}
// Need to update the scrollView size
CGSize contentSize = CGSizeMake(currentXOffset, CGRectGetHeight(self.scrollView.bounds));
self.scrollView.contentSize = contentSize;
}
輸出爲:
Newspaper[89217:a0b] Last rendered glyph 711
Newspaper[89217:a0b] Textview length 2585
Newspaper[89217:a0b] Glyph rect {{121.556, 515.3761}, {13.444, 14.315979}}
Newspaper[89217:a0b] Last rendered glyph 1441
Newspaper[89217:a0b] Textview length 2585
Newspaper[89217:a0b] Glyph rect {{129.15199, 515.3761}, {5.8480072, 14.315979}}
Newspaper[89217:a0b] Last rendered glyph 2155
Newspaper[89217:a0b] Textview length 2585
Newspaper[89217:a0b] Glyph rect {{111.80001, 515.3761}, {23.199989, 14.315979}}
Newspaper[89217:a0b] Last rendered glyph 2585
Newspaper[89217:a0b] Textview length 2585
Newspaper[89217:a0b] Glyph rect {{92.720001, 329.26801}, {3.552002, 14.31601}}
雕文矩形並不看起來不錯(我很感興趣地看到它正常工作) 但你得到顯示的最新字形。
希望它有幫助!
我覺得你最好的選擇將不受文字的'length'而是由'height'走了。你可以從'[NSString sizeWithAttributes:(NSDictionary *)attributes]' –
'得到這個特性根據你的問題,你可能不得不使用'sizeWithAttributes'以及一些自定義的「chunkenizing」工具,pbb二進制搜索到哪裏裁剪字符串。 –
我不確定這一點,但是在評估何時移動到下一個textView時,textView的contentSize應該不是一個有效值?我猜'粘貼'行動可能會導致問題。 – n00bProgrammer