【问题标题】:How to disable text color in NSTextView?如何在 NSTextView 中禁用文本颜色?
【发布时间】:2015-06-17 16:48:52
【问题描述】:

我已经设置了一个 NSColorPanel,用于更改窗口的背景颜色。在窗口中还有一个带有一些文本的 NSTextView。问题是,当我更改背景颜色时,文本颜色会变为相同。我试图找到防止这种情况的方法,但没有成功。

【问题讨论】:

  • 你是如何阻止它的?你的 textview 是 firstResponder 吗?根据changeColor: 上的NSColorPanel 文档,它将被发送到firstResponder,因此您的textview 可能会捕获此事件。
  • @Sega-Zero ,是的,textview 是第一响应者。
  • 好吧,然后尝试将另一个控件临时设置为 firstResponder,或者使用空白 changeColor: 方法子类化您的文本视图。
  • 你说的第二个想法听起来不错。
  • 相信如果您设置了颜色面板的目标和动作,它会将其动作发送到所选目标,而不是将默认的-changeColor:动作发送给第一响应者.

标签: macos cocoa nstextview nscolorpanel


【解决方案1】:

重写 NSTextView changeColor: 方法,并在该方法中保留一个到 backgeoundView 的出口,而不是更改 textView 自己的颜色,将其发送到出口:

IBOutlet NSView *changeColorTarget;

...

- (IBAction)changeColor:(id)sender {
    if(self.changeColorTarget) {    
        [NSApp sendAction:@selector(changeColor:) target:self.changeColorTarget forEvent:nil];
    } else {
        [super changeColor:sender];    
    }
}

--> 这样,textview 将保留其原始功能,除非您设置了 outlet

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-17
    • 2011-06-23
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多