Custom-drawn tokens in NSTokenField

不打扰是莪最后的温柔 提交于 2021-01-27 12:29:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!