我一直在到处找,不知道该怎么做。让我说我有一根这样的绳子:
NSString *string = @"&foo !foo";我想要做的是区分文本视图中的两个foos (听起来很有趣)。当我点击一个,我想知道哪一个(或!)先于它。我是怎么得到这个词的:
UITapGestureRecognizer *recognizer = (UITapGestureRecognizer *)sender;
UITextView *TV = (UITextView*)recognizer.view;
CGPoint location = [recognizer locationInView:TV];
location.y += TV.contentOffset.y;
UITextPosition *tapPosition = [TV closestPositionToPoint:CGPointMake(location.x, location.y)];
UITextRange *textRange = [TV.tokenizer rangeEnclosingPosition:tapPosition
withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
NSString *tappedWord = [TV textInRange:textRange];我不知道怎么得到前一个角色。TV.tokenizer UITextGranularityCharacter只收到最接近的字母,而不是符号。有什么解决办法吗?
发布于 2014-05-12 11:43:13
检查:Convert selectedTextRange UITextRange to NSRange将UITextRange转换为NSRange (我将称之为selectedRange)。
用前面关于链接问题的答案激励我,您需要在代码的末尾添加以下内容:
UITextPosition *beginning = TV.beginningOfDocument;
UITextPosition *selectionStart = textRange.start;
UITextPosition *selectionEnd = textRange.end;
NSInteger location = [TV offsetFromPosition:beginning toPosition:selectionStart];
NSInteger length = [TV offsetFromPosition:selectionStart toPosition:selectionEnd];
NSRange selectedRange = NSMakeRange(location,lenght);
NSRange range = NSMakeRange(selectedRange.location-1,1) //Maybe need to check if selectedRange.location>0
NSString *yourCharString = [[TV text] substringWithRange:range];https://stackoverflow.com/questions/23607883
复制相似问题