NSTokenField with both non-editable and editable tokens
NSTokenField with both non-editable and editable tokens
- Subject: NSTokenField with both non-editable and editable tokens
- From: Wouter Schouten <email@hidden>
- Date: Wed, 21 Oct 2009 09:59:20 +0200
I'd like to be able to type in text and drag readonly elements (tokens) into
a NSTokenField to create a custumizable string format. Like in Apple iWorks
Numers' customize cell format. The user can drag readonly NSTokenCells into
a NSTokenTextField (remaining NSRoundedTokenStyle) and type in plain text
(remaining NSPlainTextTokenStyle). I Subclassed NSTokenFieldCell to a claas
FormatToken so that I can filter out plain text tokens and my FormatToken by
using isKindOfClass: [FormatToken class] (see code below). I'm able to add
my predefined token showing up as a RoundedTokens and add plan text showing
up as PlainTextTokens. The problem is that when I double click the
FormatToken token, NSTokenField jumps to the editing mode and makes it a
normal plain text string. My FormatToken is being replaced by a
NSTokenFieldCell and remains displayed as NSPlainTextTokenStyle. I did set
the setEditable:NO for my FormatToken but it seems when the NSTokenField is
editable, all the Cell are forced editable. Does anyone know how to fix this
problem?
AppDelegate.m
- (void)applicationDidFinishLaunching: (NSNotification *)notification
{
[tokens setTokenStyle: NSPlainTextTokenStyle];
}
- (NSString *)tokenField:(NSTokenField *)tokenField
editingStringForRepresentedObject:(id)representedObject{
NSString* string;
if ([representedObject isKindOfClass: [FormatToken class]]) {
FormatToken* token = representedObject;
string = [token text];
}
else
string = representedObject;
return string;
}
- (NSArray *)tokenField:(NSTokenField *)tokenField
shouldAddObjects:(NSArray*)_tokens atIndex:(
unsigned)index
{
NSEnumerator* enumerator = [_tokens objectEnumerator];
id anObject;
while (anObject = [enumerator nextObject]) {
NSLog (@"%@", [anObject description]);
}
return _tokens;
}
- (NSString *)tokenField:(NSTokenField *)tokenField
displayStringForRepresentedObject:(id)representedObject
{
NSString* string;
if ([representedObject isKindOfClass: [FormatToken class]]) {
FormatToken* token = representedObject;
string = [token text];
}
else
string = representedObject;
return string;
}
- (NSTokenStyle)tokenField:(NSTokenField *)tokenField
styleForRepresentedObject:(id)representedObject
{
if ([representedObject isKindOfClass: [FormatToken class]])
return NSRoundedTokenStyle;
else
return NSPlainTextTokenStyle;
}
- (IBAction)addReadOnlyFromatToken: (id)sender;
{
NSMutableArray* array = [NSMutableArray arrayWithArray:[tokens objectValue
]];
FormatToken* token = [[FormatToken alloc] init];
[token setText: @"Readonly Token"];
[token setEditable: NO]
[array addObject:token];
[tokens setObjectValue: array];
}
FormatToken.m
@implementation FormatToken
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:name];
}
- (id)initWithCoder:(NSCoder *)decoder
{
name = [[decoder decodeObject] retain];
return self;
}
- (void)setText:(NSString *)text
{
name = [[NSString alloc] initWithString: text];
}
- (NSString *)text
{
return name;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden