【问题标题】:Use NSFontPanel to change foreground and background color of text in WebView使用 NSFontPanel 更改 WebView 中文本的前景色和背景色
【发布时间】:2018-03-07 18:26:15
【问题描述】:

应用:

我在 Mac App Store 中有一个 Mac 应用程序,其中嵌入了 WebView。 WebView 包含一个contenteditable DIV,充当富文本编辑器。

目标:

我想通过NSColorWellNSColorPanel机制改变WebView中选中文本的foregroundbackground color

问题:

当我单击NSColorPanel 中的颜色时,NSColorPanel 会自动在第一响应者上调用changeColor:

第一个响应者恰好是一个私有 WebKit 类,代表我的内容可编辑 DIV:WebHTMLView

上述机制总是改变所选文本的前景色。我需要一种方法来决定是否应该更改前景色或背景色。

理想的解决方案:

如果NSColorPanelNSColorWell 可以配置为不自动在第一响应者上调用changeColor: 并让我的代码处理颜色变化,那就太好了。

尝试并失败:

【问题讨论】:

  • NSFontPanelNSColorPanelNSColorPicker?
  • @Willeke 不是重复的,您引用的问题中的解决方案仅适用于 NSTextView (usesFontPanel = false),我使用的是 WebView。我编辑了我的问题。

标签: cocoa webkit nscolorpanel nscolorwell


【解决方案1】:

要拦截changeColor:,设置web视图的editingDelegate,实现WebEditingDelegate方法webView:doCommandBySelector:

例如:

- (BOOL)webView:(WebView *)webView doCommandBySelector:(SEL)selector {
    if (selector == @selector(changeColor:)) {
        if (self.foregroundColorWell.active)
            return NO;
        return YES;
    }
    return NO;
}

【讨论】:

  • 这听起来很有希望,但恐怕行不通。我可以拦截并拒绝 changeColor: ,但文本颜色仍然会改变。我怀疑处理 contenteditable DIV 的内部视图仍然实现 changeColor: 并且不尊重编辑委托。我愿意接受建议。
猜你喜欢
  • 2011-04-27
  • 2013-08-30
  • 2012-01-15
  • 1970-01-01
  • 2012-04-30
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多