NSTokenField 具有混合令牌/字符串输入,可能吗?
当在 Mail 的 NSTokenField 中输入无效电子邮件时,会得到以下结果(令牌的混合和纯字符串值):
有没有任何推荐的方法可以实现此目的?
NSTokenField 是正确的工具吗?或者我会滥用它吗?
在这个特定的项目中,我需要允许用户输入文件名模式
(不过还有其他几个用例),支持预定义令牌。
现在我要求输入如下:
Glue Text %[Tag]Other Glue Text%[Another Tag]More Text
我想将其更改为一些简单的图形解决方案,如下所示:
NSTokenField 总是(!)将输入的文本转换为标记。
要么我在网络搜索中使用了错误的关键字,
或者我真的是第一个需要这种(混合)行为的人?!
我确实阅读了 Apple 的 NSTokenField Guide,但找不到有关我的问题的任何信息。
When entering an invalid email in Mail's NSTokenField one get's this (a mix of token and plain string values):
Is there any recommendable way to accomplish this?
Is NSTokenField even the right tool for this? Or would I be abusing it?
In this particular project I need to allow the user to enter a file name pattern
(there are several other use cases though), with support for predefined tokens.
Right now I'm requiring the input to be entered like this:
Glue Text %[Tag]Other Glue Text%[Another Tag]More Text
I'd like to change this to some fool-proof graphical solution like this:
NSTokenField always(!) turns entered text into tokens.
Either I'm using the wrong keywords in my web searches,
or I'm really the first to need this (mixed) behaviour?!
I did read thru Apple's NSTokenField Guide, but couldn't find any info on my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要实现委托方法
tokenField:styleForRepresentedObject:
来返回标记的NSRoundedTokenStyle
或其他文本的NSPlainTextTokenStyle
。令牌所表示的对象是令牌字符串本身,除非您的委托返回其他对象。这应该适合您的情况:
You need to implement the delegate method
tokenField:styleForRepresentedObject:
to return eitherNSRoundedTokenStyle
for tokens orNSPlainTextTokenStyle
for other text. The represented object for an token is the token string itself, unless your delegate returns other objects.This should do the trick for your case:
实际上,您首先必须定义一个标记字符,在您的情况下将是 %
输入字符串也需要更改为:
...以便 Cocoa 知道令牌在哪里结束。
而如果你想让[Tag]在token字段中显示为Tag,你还需要实现
tokenField:displayStringForRepresentedObject:
方法:但是,这有一个很大的缺点:如果你复制或只是移动一个令牌,Cocoa 将调用
tokenField:displayStringForRepresentedObject:
并且复制/移动的令牌将更改为常规文本 Tag 而不是令牌 [Tag]。如果有人能解决上述问题,我很乐意阅读。
Actually, you first have to define a tokenizing character, which in your case would be %
The input string needs to changed as well into:
... so that Cocoa knows where the token ends.
And if you want [Tag] to be displayed as Tag in the token field, you also need to implement the
tokenField:displayStringForRepresentedObject:
method:However, this has a big drawback : if you copy or just move a token, Cocoa will call
tokenField:displayStringForRepresentedObject:
and the copied/moved token will be changed to regular text Tag instead of the token [Tag].If someone has a solution to the above problem, I'd be happy to read it.