Actually, you first have to define a tokenizing character, which in your case would be %
[tokenField setTokenizingCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@"%%"]];
The input string needs to changed as well into:
Glue Text %[Tag]%Other Glue
Text%[Another Tag]%More Text
... 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:
- (NSTokenStyle)tokenField:(NSTokenField *)tokenField
displayStringForRepresentedObject:(id)representedObject
{
if ([representedObject rangeOfString: @"["].location == 0) {
return [(NSString*)representedObject substringWithRange:NSMakeRange(1, [(NSString*)representedObject length]-2)];
return representedObject;
}
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.