現在、段落の粒度でトークナイザーを循環させることにより、n 行を含む UITextView の各行のテキスト範囲を取得できます。残念ながら、これはテキストの m 番目の行に対する私の検索アルゴリズムが n 次であることを意味します。アルゴリズムのログを n にする以外に、範囲を見つける簡単な方法はありますか? 以下は、今のところテキスト範囲を見つける方法です。
- (UITextRange *)textRangeOfLineAtIndex:(NSUInteger)index {
UITextPosition *position = self.beginningOfDocument;
NSUInteger lineCount = 0;
while([self comparePosition:self.endOfDocument toPosition:position] == NSOrderedDescending && lineCount < index) {
position = [self.tokenizer positionFromPosition:position toBoundary:UITextGranularityParagraph inDirection:UITextStorageDirectionForward];
++lineCount;
}
return [self rangeEnclosingPosition:position withGranularity:UITextGranularityParagraph inDirection:UITextStorageDirectionForward];
}