我有一个包含5列的NSTableView
。其中一列有NSComboBoxCell
。此组合框中填充了不同颜色的字符串数。所以我使用NSAttributedStrings
在我的组合框中添加彩色字符串。我的问题是我想在我的NSComboBoxCell
中添加自动填充功能。
之前我通过继承NSString
并覆盖completedString:方法,为其所有成员为NSComboboxCell
的组合框做了这个。如何使用NSAttributedString
组合框实现相同的效果?
答案 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;
}