问题描述
在Mail的 NSTokenField 中输入无效的电子邮件时(令牌和纯字符串值):
有没有什么可推荐的方法来实现这个?
NSTokenField甚至是正确的工具吗?
在这个特定的项目中,我需要允许用户输入文件名模式
现在我要求输入如下:
Glue Text%[Tag]其他Glue Text%[另一个标签] More Text
pre>
我想把这个改成这样的一个傻瓜图形解决方案:
NSTokenField始终(!)将输入的文本转换为标记。
我在网络搜索中使用了错误的关键字,
或者我真的是第一个需要这个(混合)行为吗?!
我通过Apple的NSTokenField指南阅读,但找不到任何有关我的问题的信息。
解决方案您需要实现委托方法
tokenField:styleForRepresentedObject:
为令牌或 NSRoundedTokenStyle > 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 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:
- (NSTokenStyle)tokenField:(NSTokenField *)tokenField styleForRepresentedObject:(id)representedObject { if ([representedObject rangeOfString: @"%["].location == 0) { return NSRoundedTokenStyle; } else { return NSPlainTextTokenStyle; } }
这篇关于NSTokenField与混合令牌/字符串输入,可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!