Donovan
Donovan

Reputation: 6122

Custom-drawn tokens in NSTokenField

Is there a way to have custom tokens inside an NSTokenField? By "custom" I mean I would like to draw them in a custom way -- custom hover, background image, etc.

Upvotes: 4

Views: 2739

Answers (2)

marcprux
marcprux

Reputation: 10365

It can be done without using private APIs. An NSTokenField is merely an NSTextField that makes clever use of the support for custom attachment cell support. Customizing it involves some insight into the complex NSTextStorage/NSLayoutManager/NSAttachmentCell interactions, but one good open-source implementation of customizable tokens can be seen at: https://github.com/octiplex/OEXTokenField

Upvotes: 10

Enchilada
Enchilada

Reputation: 3919

Yes, this is possible, but you'll have to resort to using private APIs, which you don't want if you intend on publishing your app to the Mac App Store. I have contacted Apple about this, and they have confirmed that there is no way of doing this without private APIs - if one needs difference appearance using only public APIs, one must create one's own control from scratch.

If you still want to do this using private APIs, look into BWToolkit. BWTokenField uses these private APIs to modify the token appearance. That should be a good starting point. But it takes a while to figure it all out and tweak things until one gets one's desired effect.

Upvotes: 4

Related Questions