如何允许光标移动到UITextView中的任何位置

时间:2013-07-01 11:06:37

标签: objective-c cursor uitextview uitextviewdelegate

我有没有键盘的UITextView子类,它使用按钮进行输入。我想通过点击移动光标到任何位置,而不仅仅是开始或结束单词。这就是它现在的运作方式:

enter image description here

enter image description here

这就是我想要的:

enter image description here

我认为这个问题的解决方案对初学者来说非常复杂,所以我想请你详细说明一下如何做。

我找到this 但不知道我应该在哪里使用这部分代码。


这是解决方案:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event];
    UITouch *userTouch = [[event allTouches] anyObject];
    CGPoint currentPos = [userTouch locationInView:self];
    UITextPosition *cursorPosition=[self closestPositionToPoint:CGPointMake(currentPos.x, currentPos.y)];
    [self setSelectedTextRange:[self textRangeFromPosition:cursorPosition toPosition:cursorPosition]];
}

0 个答案:

没有答案
相关问题