0

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.

Community
  • 1
  • 1
GeorgeBuckingham
  • 195
  • 1
  • 1
  • 10

1 Answers1

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.

GeorgeBuckingham
  • 195
  • 1
  • 1
  • 10