iOS如何以编程方式模拟uitextview上的两次点击? [英] iOS how to simulate two taps on uitextview programmatically?

查看:20
本文介绍了iOS如何以编程方式模拟uitextview上的两次点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做,因为我想在点击一次后抓取选定的文本.如果有其他选择文本的方法,请告诉我.

I want to Do this, Because I want to grab the selected text after tapping once. If there is another approach to select text, please let me know.

谢谢.

我修改了 Luiz 解决方案,但它在 textview 上复制了整个文本.我要实现的是选择一个词.

I modified Luiz solution, but it copies whole text on textview. What I want to achieve is to select one word.

    if (gestureRecognizer.state == UIGestureRecognizerStateRecognized)
    {
        textview.selectedRange = NSMakeRange(0, textview.text.length);
        NSString *selected = [self.textview.text substringWithRange:textview.selectedRange];
        NSLog (@"selected = %@", selected);

    }

<小时>

我仍在努力为这项任务找到一个运行良好的代码.在 Kindle.app 和 Wakaru.app 等大型应用程序中,可以轻按一次并强制选择一个单词,就像轻按两次一样.您知道如何做到这一点或实现此功能的想法吗?

I am still working to find a well-working code for this task. In big apps like Kindle.app and Wakaru.app, it is possible to tap once and force selecting one word like two taps would do. Do you know how to do this or an idea to achieve this function?

推荐答案

请尝试执行以下步骤:

  1. 为您的 UITextView 设置一个 IBOutlet,例如,myTextView.

在视图控制器的 viewDidLoad 上,为 myTextView 创建一个 UITapGestureRecognizer:

On viewDidLoad of your view controller, create an UITapGestureRecognizer for myTextView:

UITapGestureRecognizer *tapTextView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[myTextView addGestureRecognizer:tapTextView];
[tapTextView release];

  • 实现 tap 方法,使 UITextView 选择它的文本:

  • Implement the tap method so it makes the UITextView selects its text:

     - (void)tap:(UITapGestureRecognizer *)recognizer
     {
         if (recognizer.state == UIGestureRecognizerStateRecognized) {
             myTextView.selectedRange = NSMakeRange(0, myTextView.text.length);
         }
     }
    

  • 我希望这会有所帮助!

    最好的问候!

    这篇关于iOS如何以编程方式模拟uitextview上的两次点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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