1

我的目标是使文本选择行为类似于 Apple Pages、MS Word 和任何其他文本处理器,当 NSTextView 仅选择文本字形而不选择任何缩进或整个 NSTextContainer 大小时,就像默认 NSTextView 的行为一样。

考虑到这个目标,我将 NSLayoutManager 子类化并覆盖func rectArray(forCharacterRange charRange: NSRange, withinSelectedCharacterRange selCharRange: NSRange, in container: NSTextContainer, rectCount: UnsafeMutablePointer<Int>) -> NSRectArray?

在我的覆盖中,我只是计算了只绑定字形并拒绝任何缩进、空格等的新矩形。

一切正常。但突然在 macOS 10.11 中,Apple 确实弃用了我正在使用的方法。并建议func enumerateEnclosingRectsForGlyphRange: withinSelectedGlyphRange: inTextContainer: usingBlock:改用。

但似乎这种新方法不像以前不推荐使用的方法那样起作用。不可能再自定义封闭的矩形了。我所能做的就是通过 rects TextKit 的计算来枚举。现在不可能实现理想的文本选择行为,因为矩形是只读的。顺便说一下,TextKit 根本不会调用新方法。所以我没有必要去想它。

我应该怎么办?

4

1 回答 1

1

虽然非等效方法替换的惊喜仍然存在,但我还是找到了解决方案。

事实证明,我不需要rectArrayForCharacterRange方法来完成这项工作。我只是覆盖func fillBackgroundRectArray(_ rectArray: UnsafePointer<NSRect>, count rectCount: Int, forCharacterRange charRange: NSRange, color: NSColor)并将我的矩形计算放在那里。

现在一切都像魅力一样!

于 2017-06-30T21:00:41.263 回答