如何在具有NSAttributedStrings的NSComboBox中添加自动完成功能

时间:2014-02-26 06:49:13

标签: objective-c macos cocoa

我有一个包含5列的NSTableView。其中一列有NSComboBoxCell。此组合框中填充了不同颜色的字符串数。所以我使用NSAttributedStrings在我的组合框中添加彩色字符串。我的问题是我想在我的NSComboBoxCell中添加自动填充功能。

之前我通过继承NSString并覆盖completedString:方法,为其所有成员为NSComboboxCell的组合框做了这个。如何使用NSAttributedString组合框实现相同的效果?

1 个答案:

答案 0 :(得分:0)

仅使用completedString:方法并使用NSString。要更改颜色,请创建NSValueTransformer绑定并使用以下代码。我希望它有所帮助。 `

+ (Class)transformedValueClass{

    return [NSAttributedString class];
}

+ (BOOL)allowsReverseTransformation{

    return NO;
}

- (id)transformedValue:(id)value{

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSColor redColor],NSForegroundColorAttributeName,nil];

    NSAttributedString *str = [[NSAttributedString alloc] initWithString:value attributes:attributes];

    return str;
}