我已经在我的xib中创建了一个NSTokenField。我想在tokenField中显示不同颜色的令牌。含义:一些令牌将是蓝色的,其余的将是红色的(根据其内容)。这有可能吗?
下面的代码对我不起作用。我希望有人能帮助我:
- (id)tokenField:(NSTokenField *)tokenField representedObjectForEditingString:(NSString *)editingString
{
id returnRepresentedObject = nil;
NSTokenFieldCell *tf = [[NSTokenFieldCell alloc] init];
tf.stringValue = editingString;
tf.backgroundColor = [NSColor redColor];
returnRepresentedObject = tf;
return returnRepresentedObject;
}结果:所有标记都保持为蓝色...:-(
任何帮助我们都将不胜感激!
发布于 2013-07-29 19:39:14
可以通过使用私有API来实现。子类NSTokenAttachmentCell (私有)和NSTokenFieldCell。

使用BWTokenAttachmentCell和BWTokenFieldCell类以及来自BWToolkit的NSTokenAttachmentCell类转储。修改BWTokenAttachmentCell的初始化方法。
[示例项目
备注:
如果您的目标不是Mac App Store,请使用此方法。
发布于 2015-07-27 23:19:50
你可能不得不扮演你自己的角色。有一个2010年的wwdc视频是关于高级Cocoa文本处理的。NSTokenField使用NSTextAttachments来呈现令牌。
https://stackoverflow.com/questions/17672413
复制相似问题