How to get range of characters from a tapped word UITextview

坚强是说给别人听的谎言 提交于 2019-12-12 01:57:31

问题


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.


回答1:


The solution I have used is:

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.



来源:https://stackoverflow.com/questions/15473777/how-to-get-range-of-characters-from-a-tapped-word-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!