NSScrollView + NSTextView. scrolling to a specific word.

I have an interesting challenge, but I do not know where to start.

given an NSTextView in an NSScrollView, and enough text in the textView to enable scrolling, how do you determine the location of a single word, and jump to it?

we see this sort of behavior in Find panels in Most text editors. So it can be done.

additionally, I would very much like to find some kind of predetermined... elements in the text. as a link or an image is not text but can be inline with the text (in Attributed strings) I would like to put an element in the text that is not text, but is targetable and capable of being tagged with some kind of reference. Almost like a bookmark, in which I can scroll to. I have determined that I could use URL Links. But they are so fragile.

determine the location of a single word, and jump to it?

What do you mean by jump exactly ?

There are probably many solutions, but here is one.

  • Get the lines of the TextView (line being a visual line in textView, not a line separated by linefeed)

-> solution here https://stackoverflow.com/questions/21090331/how-can-i-read-uitextview-text-line-by-line-ios and some other info https://developer.apple.com/forums/thread/8247

  • find the line in which the word is present

-> just search line by line

  • Then you have the position of the word, both as line / range in line and as geometry (line number)

-> jump to it

Hope that helps.

NSScrollView + NSTextView. scrolling to a specific word.
 
 
Q