将 NSTextField 的文本颜色绑定到 NSColorWell 吗?

发布于 2024-11-05 09:09:37 字数 1081 浏览 0 评论 0原文

我对 Cocoa Bindings 还很陌生,但我已经看到了足够多的东西,我很想把我所有旧的笨重方法改成它。例如,我有一个 NSColorWell 可以更改视图中某些 NSTextField 的文本颜色。实践起来看似简单,但行不通。

以下是我的 NSColorWell 的绑定:

在此处输入图像描述

这是我的 NSColorWell 的绑定NSTextField:

在此处输入图像描述

但它不显示颜色,而是显示 NSCalibrateRGBColor ...。显然它没有设置字段的颜色值,它只是显示原始数据。

因此,在摸索之后,我尝试通过执行以下操作来制作自己的 NSValueTransformer:

@interface DataToColor: NSValueTransformer {}
@end
#import "QWDataToColor.h"
@implementation DataToColor
+(Class)transformedValueClass { return [NSColor class]; }
+(BOOL)allowsReverseTransformation { return NO; }
-(id)transformedValue:(id)item {
    NSColor *theColor=nil;
    NSData *theData=[NSData dataWithData:item];
    if (theData != nil)
        theColor=(NSColor *)[NSUnarchiver unarchiveObjectWithData:theData];
    return theColor;
}
@end

然后我将该值转换器设置为 IB 中绑定中的“值转换器”区域。

然而,它仍然给出了相同的结果。有什么想法吗?

I'm quite new to Cocoa Bindings, but I've seen enough that I'd love to change all my old clunky methods over to it. For example, I have a NSColorWell that changes the text color of some NSTextFields in my view. Seems easy in practice, but it's not working.

Here's how my bindings look for my NSColorWell:

enter image description here

And here's my bindings for my NSTextField:

enter image description here

But instead of displaying a color it just displays NSCalibratedRGBColor.... Obviously it's not setting the value of the color to the field, it's just displaying the raw data.

So, after poking around I tried to make my own NSValueTransformer by doing this:

@interface DataToColor: NSValueTransformer {}
@end
#import "QWDataToColor.h"
@implementation DataToColor
+(Class)transformedValueClass { return [NSColor class]; }
+(BOOL)allowsReverseTransformation { return NO; }
-(id)transformedValue:(id)item {
    NSColor *theColor=nil;
    NSData *theData=[NSData dataWithData:item];
    if (theData != nil)
        theColor=(NSColor *)[NSUnarchiver unarchiveObjectWithData:theData];
    return theColor;
}
@end

Then I set that value transformer to my "Value Transformer" area in my bindings in IB.

However, it still gave the same results. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱,才寂寞 2024-11-12 09:09:37

值绑定是:

An NSString or NSNumber that is displayed as the content of the NSTextField

您想要绑定 NSTextField 的 textColor 属性,而不是值。

请参阅 http://developer.apple.com /library/mac/#documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSTextField.html 了解 NSTextField 支持的绑定的完整列表。

The value binding is:

An NSString or NSNumber that is displayed as the content of the NSTextField

You want to bind the textColor property of your NSTextField, not value.

See http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSTextField.html for the complete list of bindings supported by NSTextField.

记忆消瘦 2024-11-12 09:09:37

这很简单。您可以通过 IB 同等地绑定代码中的内容,在这种情况下,我认为这是唯一的方法,因为视图(在这种情况下,颜色井)不会在 IB* 中显示为可绑定对象:

// Given two appropriately-set-up IBOutlets, tf to the text field,
// and cw to the color well
[tf bind:@"textColor" toObject:cw withKeyPath:@"color" options:nil];

也可以,并且在某些情况下需要,例如当您可能需要在其他地方使用该值或将其存档时,通过控制器中的中间变量绑定每个对象。


*我很乐意在这方面被证明是错误的!

This is pretty simple. You can bind things in code equally as well as through IB, and in this case I think this is the only way to do it, because views (in this case, the color well) don't show up as bindable objects in IB*:

// Given two appropriately-set-up IBOutlets, tf to the text field,
// and cw to the color well
[tf bind:@"textColor" toObject:cw withKeyPath:@"color" options:nil];

It's also possible, and in some cases desirable, such as when you might need to use this value in other places or archive it, to bind each object through an intermediate variable in a controller.


*I would love to be shown wrong about this!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文