NSTokenField与混合令牌/字符串输入,可能?

编程入门 行业动态 更新时间:2024-10-27 19:15:35
本文介绍了NSTokenField与混合令牌/字符串输入,可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Mail的 NSTokenField 中输入无效的电子邮件时(令牌和纯字符串值):

有没有什么可推荐的方法来实现这个? NSTokenField甚至是正确的工具吗?

在这个特定的项目中,我需要允许用户输入文件名模式

现在我要求输入如下:

Glue Text%[Tag]其他Glue Text%[另一个标签] More Text pre>

我想把这个改成这样的一个傻瓜图形解决方案:

NSTokenField始终(!)将输入的文本转换为标记。

我在网络搜索中使用了错误的关键字, 或者我真的是第一个需要这个(混合)行为吗?!

我通过Apple的NSTokenField指南阅读,但找不到任何有关我的问题的信息。

解决方案

您需要实现委托方法 tokenField:styleForRepresentedObject:为令牌或 NSPlainTextTokenStyle 以获取其他文本。代表的表示对象是令牌字符串本身,除非您的委托返回其他对象。

这样做对您的案件应该有帮助:

- NSTokenStyle)tokenField:(NSTokenField *)tokenField styleForRepresentedObject:(id )resentObject { if([referencedObject rangeOfString:@%[]。location == 0){ return NSRoundedTokenStyle; } else { return NSPlainTextTokenStyle; } }

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.

解决方案

You need to implement the delegate method tokenField:styleForRepresentedObject: to return either NSRoundedTokenStyle for tokens or NSPlainTextTokenStyle 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:

- (NSTokenStyle)tokenField:(NSTokenField *)tokenField styleForRepresentedObject:(id)representedObject { if ([representedObject rangeOfString: @"%["].location == 0) { return NSRoundedTokenStyle; } else { return NSPlainTextTokenStyle; } }

更多推荐

NSTokenField与混合令牌/字符串输入,可能?

本文发布于:2023-10-29 22:45:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1540986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   字符串   NSTokenField

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!