如何从敲击的单词UITextview获取字符范围 [英] How to get range of characters from a tapped word UITextview

查看:141
本文介绍了如何从敲击的单词UITextview获取字符范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从UITextView的轻敲单词中获取字符范围.我已经在UITextView *tv实例上设置了UITapGestureRecognizer.

I need to get the range of characters from a tapped word from a UITextView. I have set up a UITapGestureRecognizer on my instance of UITextView *tv.

我目前有一个解决方案,可以告诉我被窃听的单词,如以下解决方案中所述:

I currently have a solution that can tell me the word that was tapped, as described in this solution: Get word from tap in UITextView.

我需要敲击的单词的字符范围,而不仅仅是单词本身,最好以可以访问整数的开始和结束值的方式使用.

I need the range of characters of the word that was tapped, not just the word itself, preferably available in a way where I can access the start and end values as integers.

推荐答案

我使用的解决方案是:

int posOfSelected = [tv offsetFromPosition:tv.beginningOfDocument
                        toPosition:textRange.start];

我得到了所敲击单词的第一个字母的字符索引,作为与文档开头的偏移量.

I get the character index of the first letter of the tapped word, as an offset from the beginning of the document.

这篇关于如何从敲击的单词UITextview获取字符范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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