Right now, I am able to get the text ranges of each line in a UITextView containing n lines by using cycling through the tokenizer with paragraph granularity. Unfortunately, that means my search algorithm for the m-th line in the text is of order n. Is there any easier way for me to find the range other than making my algorithm log n? The following is how I find my text range for now:
- (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];
}