如何将UITextView的光标移动到TOUCHED LOCATION? [英] How to move cursor of UITextView to TOUCHED LOCATION?

查看:117
本文介绍了如何将UITextView的光标移动到TOUCHED LOCATION?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题的目标是制作iOS应用笔记应用。
iOS的笔记应用程序的一点是,它们为用户提供使用超链接或电话号码链接或电子邮件地址作为蓝色文本颜色,我们可以同时编辑文本。

The goal of my question is making note app like iOS's. The point of iOS's note app is that they provide to user to use hyper link or phone number link or email address as blue textcolor, and we can edit text at the same time.

通常,当我们按下UITextView时,它会将光标定位到我们触摸的位置。但是,如果我使用UITextView的dataDetectorTypes来设置电话号码或http链接(如iOS的笔记应用程序),我应该将其设置为不可编辑,否则链接不起作用。
这意味着我无法单击UITextView和编辑文本。

Normally, When we press UITextView, it locate cursor to where we touched. but If I'm using dataDetectorTypes of UITextView to put things like phone number or http links like iOS's note app, I should set it non-editable, otherwise link doesn't work. It means I can't click UITextView and Edit text.

但我可以通过使用以下代码将其设置为可编辑

but I could set it editable by using code below

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
    action:@selector(editTextRecognizerTabbed:)];

-

- (BOOL) editTextRecognizerTabbed:(UITapGestureRecognizer *) aRecognizer
{
    textview.editable = YES;
    textview.dataDetectorTypes = UIDataDetectorTypeNone;
    [textview becomeFirstResponder];
}

现在我可以编辑文本,一切正常,但问题是它始终开始文本结束,因此用户应再次点击他们想要写的位置。我理解其中的原因。

and now I could edit text and everything works fine, but problem is that it always start end of text, so user should tap once again to where they want to write. and I understand the reason.

现在我想将光标移动到我在第一点按下的位置,然后使用[aRecognizer locationInView:view]获得坐标;

NOW I want to move cursor to location where I pressed at the first point and I got the coordinate by using [aRecognizer locationInView:view];

如何将UITextView中的游标移动到位置我从认可者那里获得?

SO HOW TO MOVE CURSOR in UITextView TO LOCATION I GOT FROM RECOGNIZER?

谢谢。

推荐答案

- (void) editTextRecognizerTabbed:(UITapGestureRecognizer *) aRecognizer
{
    if (aRecognizer.state==UIGestureRecognizerStateEnded)
    {
        //Not sure if you need all this, but just carrying it forward from your code snippet
        textview.editable = YES;
        textview.dataDetectorTypes = UIDataDetectorTypeNone;
        [textview becomeFirstResponder];

        //Consider replacing self.view here with whatever view you want the point within
        CGPoint point = [aRecognizer locationInView:self.view];
        UITextPosition * position=[textView closestPositionToPoint:point];
        [textView setSelectedTextRange:[textView textRangeFromPosition:position toPosition:position]];
    }
}

这篇关于如何将UITextView的光标移动到TOUCHED LOCATION?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆