diff --git a/Sources/STTextView/STTextView+CopyPaste.swift b/Sources/STTextView/STTextView+CopyPaste.swift index b48d653..0a17c6f 100644 --- a/Sources/STTextView/STTextView+CopyPaste.swift +++ b/Sources/STTextView/STTextView+CopyPaste.swift @@ -21,7 +21,8 @@ extension STTextView { return } - let nsrange = NSRange(textLayoutManager.insertionPointLocation!, in: textContentStorage) + let insertLocation = textLayoutManager.insertionPointLocation ?? textLayoutManager.documentRange.location + let nsrange = NSRange(insertLocation, in: textContentStorage) insertText(string, replacementRange: nsrange) } diff --git a/Sources/STTextView/STTextView.swift b/Sources/STTextView/STTextView.swift index 3fe31ff..2ef6af2 100644 --- a/Sources/STTextView/STTextView.swift +++ b/Sources/STTextView/STTextView.swift @@ -418,14 +418,21 @@ extension STTextView: NSTextViewportLayoutControllerDelegate { } func updateSelectionHighlights() { - guard !textLayoutManager.textSelections.isEmpty else { return } + guard !textLayoutManager.textSelections.isEmpty else { + selectionView.subviews = [] + return + } selectionView.subviews = [] - + for textSelection in textLayoutManager.textSelections { for textRange in textSelection.textRanges { textLayoutManager.enumerateTextSegments(in: textRange, type: .highlight, options: []) {(textSegmentRange, textSegmentFrame, baselinePosition, textContainer) in var highlightFrame = textSegmentFrame.intersection(frame) + guard !highlightFrame.isNull else { + return true + } + let highlight = STTextSelectionView() highlight.wantsLayer = true