Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

objective c - NSTokenField with mixed token/string input, possible?

When entering an invalid email in Mail's NSTokenField one get's this (a mix of token and plain string values):

alt text

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: alt text

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...