我发现 hamstergene 的回答不正确。事实上,NSTextView 会在其边界上突出显示其文本容器行。
因此,如果您使用段落的头部缩进,那么前导文本空白区域将突出显示。如果您选择 EOL 字符,则文本容器的尾部将突出显示。
我的解决方案是取消段落样式的首尾缩进(我将它们缓存在私有变量中,并在访问我的文本存储进行打印时将它们放回去)并通过覆盖 lineFragmentRectForProposedRect: atIndex: writingDirection: remainingRect 我的 NSTextContainer 子类的方法。
但后来我找到了很多正确的方法。只需覆盖 NSLayoutManager 的func fillBackgroundRectArray(_ rectArray: UnsafePointer<NSRect>, count rectCount: Int, forCharacterRange charRange: NSRange, color: NSColor),计算您的矩形并使用这些矩形调用 super。如果您正确计算了选择矩形,您将获得与 Apple Pages 或 MS Word 中一样的准确选择行为。
简单易行!
更新
这是我计算选择矩形的代码:
public override func fillBackgroundRectArray(_ rectArray: UnsafePointer<CGRect>, count rectCount: Int, forCharacterRange charRange: NSRange, color: OSColor) {
// if characters are selected, make sure that we draw selection of those characters only, not the whole text container bounds
guard let textView = textContainer(forCharacterIndex: charRange.location)?.textView,
NSIntersectionRange(textView.selectedRange(), charRange).length > 0,
let textStorage = self.textStorage as? ParagraphTextStorage else {
super.fillBackgroundRectArray(rectArray, count: rectCount, forCharacterRange: charRange, color: color)
return
}
let selectedGlyphRange = self.glyphRange(forCharacterRange: charRange, actualCharacterRange: nil)
var selectionRectArray: [CGRect] = []
enumerateLineFragments(forGlyphRange: selectedGlyphRange) { (rect, usedRect, textContainer, glyphRange, stop) in
let lineCharRange = self.characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil)
let intersection = NSIntersectionRange(charRange, lineCharRange)
// if selected all characters of the line, then we already have teir layout rects
if intersection == lineCharRange {
let paragraphIndex = textStorage.paragraphIndex(at: intersection.location)
let paragraphRange = textStorage.paragraphRanges[paragraphIndex]
let hasNewLineChar = lineCharRange.max == paragraphRange.max && paragraphRange.max < textStorage.length ||
paragraphRange.max == lineCharRange.max && intersection.max == textStorage.length && paragraphIndex < textStorage.paragraphRanges.count - 1
let newLineCharSize = hasNewLineChar ? self.newLineCharSize : .zero
let lineRect = CGRect(x: usedRect.origin.x + textView.textContainerInset.width + textContainer.lineFragmentPadding,
y: usedRect.origin.y + textView.textContainerInset.height - (rect.height - usedRect.height),
width: usedRect.width + newLineCharSize.width - textContainer.lineFragmentPadding * 2,
height: rect.height)
selectionRectArray.append(lineRect)
} else {
// calculate rect for partially selected characters of the line
let partialRect = self.usedLineRect(forCharacterRange: intersection, in: textContainer)
selectionRectArray.append(partialRect)
}
}
super.fillBackgroundRectArray(selectionRectArray, count: selectionRectArray.count, forCharacterRange: charRange, color: color)
}
public func usedLineRect(forCharacterRange charRange: NSRange, in textContainer: NSTextContainer) -> CGRect {
guard let textView = textContainer.textView, let textStorage = textStorage as? ParagraphTextStorage else { return .zero }
let glyphRange = self.glyphRange(forCharacterRange: charRange, actualCharacterRange: nil)
let textContainer = self.textContainer(forGlyphAt: glyphRange.location, effectiveRange: nil) as! ModernTextContainer
let paragraphIndex = textStorage.paragraphIndex(at: charRange.location)
let paragraphRange = textStorage.paragraphRanges[paragraphIndex]
let hasNewLine = paragraphRange.max == charRange.max && charRange.max < textStorage.length ||
paragraphRange.max == charRange.max && charRange.max == textStorage.length && paragraphIndex < textStorage.paragraphRanges.count - 1
let newLineCharSize = hasNewLine ? self.newLineCharSize : .zero
// if new line is in range, boundingRect will return the whole width of the text container, fix that
let noNewLineGlyphRange = hasNewLine ? NSRange(location: glyphRange.location, length: glyphRange.length - 1) : glyphRange
let charRect = boundingRect(forGlyphRange: noNewLineGlyphRange, in: textContainer)
let lineRect = lineFragmentRect(forGlyphAt: noNewLineGlyphRange.location, effectiveRange: nil, withoutAdditionalLayout: true)
#if os(macOS)
// respect the flipped coordinate system with abs function
let rect = CGRect(x: charRect.origin.x + textView.textContainerInset.width,
y: abs(charRect.origin.y + textView.textContainerInset.height - (lineRect.height - charRect.height)),
width: charRect.width + newLineCharSize.width,
height: lineRect.height)
#else
let rect = CGRect(x: charRect.origin.x + textView.textContainerInset.left,
y: abs(charRect.origin.y + textView.textContainerInset.top - (lineRect.height - charRect.height)),
width: charRect.width + newLineCharSize.width,
height: lineRect.height)
#endif
return rect
}
这个执行速度极快的计算的重要部分是我正在使用我自己的 ParagraphTextStorage 实现。其目的是在编辑文本存储时实时计算段落范围。知道正确的段落范围,我可以在计算选定的矩形时使用整数。否则我不得不做一堆子字符串来了解是否选择了换行符。而且操作真的很慢。
我的 ParagraphTextStorage 的实现在这里:https://github.com/CineDev/ParagraphTextKit