Objective c 在UITextView中获取行范围的更好方法
现在,通过使用段落粒度的标记器循环,我能够获得包含n行的UITextView中每行的文本范围。不幸的是,这意味着我对文本中第m行的搜索算法是n阶的。除了让我的算法记录n之外,还有什么更简单的方法可以让我找到范围吗?以下是我目前如何找到我的文本范围:Objective c 在UITextView中获取行范围的更好方法,objective-c,search,line-numbers,uitextposition,Objective C,Search,Line Numbers,Uitextposition,现在,通过使用段落粒度的标记器循环,我能够获得包含n行的UITextView中每行的文本范围。不幸的是,这意味着我对文本中第m行的搜索算法是n阶的。除了让我的算法记录n之外,还有什么更简单的方法可以让我找到范围吗?以下是我目前如何找到我的文本范围: - (UITextRange *)textRangeOfLineAtIndex:(NSUInteger)index { UITextPosition *position = self.beginningOfDocument; 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];
}
-(UITextRange*)textRangeofLineIndex:(NSInteger)索引{
UITextPosition*position=self.beginingofDocument;
整数行数=0;
while([self-comparePosition:self.endOfDocument-toPosition:position]==sensorderedescending&&lineCount