Token text box, as in Numbers

I would like to have a text box, such as an expression editor text box, in Numbers:

enter image description here

It is very similar to NSTokenField , but NSTokenField only supports a delimited list of tokens, for example, the To: field in Mail.app.

enter image description here

I need to insert these tokens into the text in certain places, but otherwise they work exactly the same as NSTokenField (backspace removes the token, you can drag them, etc.).

Is there any 1st or 3rd party control that does something like this? I did not find anything.

If not, how would you recommend implementing it? Use Core Text and reinvent the wheel (implement NSTextField with better token support)? Or is there a better solution?

+7
source share
1 answer

I don’t think there is an alternative control for NSTokenField (well, I could not find it a couple of weeks ago).

A possible solution might be the following solution, presented in the Apple LayoutManagerDemo sample project. This shows a subclass of NSTextView capable of detecting mouse movement over text. Install and run the demo to get the basic idea.

The example uses NSLayoutManager to detect mouse movement, but the code can probably be adapted to detect certain characters in the text, such as tokens in the text field. When you have tokens and their location from NSLocationManager , you can insert your own view based on the characteristics of your token. A possible solution would be to use NSTextAttachmentCell , which will become a glyph inside your text. The advantage of using NSTextAttachmentCell is that it is treated as a character using the Cocoa text system (you can select it, this follows formatting, etc.). By drawWithFrame:inView: you can add various visible attributes for each token.

+5
source

All Articles