我有一个包含 5 列的 NSTableView
。其中一列具有 NSComboBoxCell
。该组合框填充了许多不同颜色的字符串。所以我使用 NSAttributedStrings 将彩色字符串添加到我的组合框中。我的问题是我想向我的 NSComboBoxCell
添加自动完成功能。
之前,我通过子类化 NSComboboxCell
并覆盖 CompletedString: 方法,将所有成员都作为 NSString
的组合框做到了这一点。如何使用 NSAttributedString
实现组合框的相同效果?
最佳答案
仅使用completedString:方法并使用NSString。要更改颜色,请创建 NSValueTransformer 绑定(bind)并使用以下代码。我希望它有帮助。 `
+ (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;
}
关于objective-c - 如何在具有 NSAttributedStrings 的 NSComboBox 中添加自动完成功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22033856/