带有自定义字段编辑器的视觉故障

问题描述 投票:0回答:1

我有一个NSTextField,我想让它自动完成以始终对字段的全部内容进行操作,而不是对当前单词进行操作。

我正在尝试通过在窗口委托中使用windowWillReturnFieldEditor()返回NSTextView子类来实现这一点,在这里我可以在rangeForUserCompletion中返回全文范围。

这实际上是可行的,除了一些绘图问题。无法绘制文本字段的聚焦环,并且该字段的旧文本覆盖在当前键入的内容上。

我需要在NSTextView上进行一些其他配置以使其作为字段编辑器正常工作吗?我认为一种替代方法是自定义构成单词边界的内容,但是我还没有找到如何做到这一点的方法。

相关代码:

extension MyWindowController: NSWindowDelegate
{
  func windowWillReturnFieldEditor(_ sender: NSWindow, to client: Any?) -> Any?
  {
    if let clientField = client as? NSTextField,
       clientField === myTextField {
      let editor = FullReplacementTextView()

      editor.isFieldEditor = true
      return editor
    }
    else {
      return nil
    }
  }
}

class FullReplacementTextView: NSTextView
{
  override var rangeForUserCompletion: NSRange
  {
    return self.string.fullNSRange // Extension method that returns the full string range
  }
}
macos cocoa nstextfield nstextview
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.