问题
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.
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/6861070/custom-drawn-tokens-in-nstokenfield