NSTokenField:令牌的不同颜色

时间:2013-07-16 09:09:53

标签: objective-c macos cocoa nstokenfield nstokenfieldcell

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

结果:所有令牌都保持蓝色......: - (

任何帮助都将受到高度赞赏!

2 个答案:

答案 0 :(得分:1)

可以使用私有API。子类NSTokenAttachmentCell(私有)和NSTokenFieldCell

Sample project

enter image description here

使用BWToolkit中的BWTokenAttachmentCell和BWTokenFieldCell类以及NSTokenAttachmentCell类转储。修改BWTokenAttachmentCell的初始化方法。

[示例项目

注意:

如果您不是针对Mac App Store,请使用此方法。

答案 1 :(得分:1)

你可能需要自己扮演角色。 2010年有一个关于高级Cocoa Text处理的wwdc视频。 NSTokenField使用NSTextAttachments来渲染令牌。

相关问题