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

标签 objective-c macos cocoa

我有一个包含 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/

相关文章:

cocoa - NSFileHandle 和 scanf 不回显 Xcode 4.5 中的输入

ios - 如何将数组 `UnsafePointer<DSPComplex>` 转换为 float 数组?

cocoa - 我们如何在 NSTextField-Cocoa 中使用 Shift+Enter 进行换行

ios - 如何在SCENEKIT中绘制无限长的线?

objective-c - iPhone App通过LinkedIn API授权后无法确认

android - 在 OS X 中开始 OpenGL 3.0+ 编程(再次),针对多个平台

macos - 使用开发者 ID 签名的 Mac 应用程序无法启动,我收到 com.apple.developer.networking.vpn.api entitlement is not allowed(错误代码 -67050)

ios - 通过 AirDrop 发送 NSArray

objective-c - 是否有用于表示 3D 点的 Cocoa 类型

macos - 如何发现 Mac OS X 上的“逻辑”核心数量?