如果布局管理器中只有一个TextContainer,那么操作系统将处理用户交互。但是有了多个文本容器,我在某个地方读到它不是开箱即用的。
以下是我已经使用过的一些没有运气的事情。
NSRange range = NSMakeRange(5, 10);
UITextPosition *from = [textView positionFromPosition:[textView beginningOfDocument] offset:range.location];
UITextPosition *to = [textView positionFromPosition:from offset:range.length];
[textView select:self];
[textView setSelectedTextRange:[textView textRangeFromPosition:from toPosition:to]];
和
NSRange range = NSMakeRange(5, 10);
textView.selectedRange = range;
如果只有一个TextContainer,我能够让它工作,但我希望它有多个容器。
这是我想以编程方式
实现的更新: 添加了我正在处理的代码
- (void)didDoubleTap:(id)sender {
UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)sender;
if (tapGesture.state == UIGestureRecognizerStateEnded) {
CGPoint point = [tapGesture locationInView:self];
for (UITextView *textView in self.textViews) {
if (CGRectContainsPoint(textView.frame, point)) {
textView.selectable = YES;
[textView becomeFirstResponder];
NSInteger glypIndex = [self.articleLayoutManager glyphIndexForPoint:point inTextContainer:textView.textContainer];
NSLog(@"GlyphIndex : %ld",glypIndex);
NSRange wordRange = [self rangeForWordWithGlyphAtIndex:glypIndex];
wordRange.length += 5;//For testing purpose
NSLog(@"Got Range");
UITextPosition *beginning = textView.beginningOfDocument;
UITextPosition *start = [textView positionFromPosition:beginning offset:wordRange.location];
UITextPosition *end = [textView positionFromPosition:start offset:wordRange.length];
UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];
textView.selectedTextRange = textRange;
textView.selectedRange = wordRange;
UITextRange *textRange1 = [textView selectedTextRange];
CGRect wordRect = [self.articleLayoutManager boundingRectForGlyphRange:wordRange inTextContainer:textView.textContainer];
NSLog(@"Got Word Rect");
UIMenuController *theMenu = [UIMenuController sharedMenuController];
[theMenu setTargetRect:wordRect inView:textView];
[theMenu setMenuVisible:YES animated:YES];
}
}
}
}
感谢。