I wanted to complete a novel reader, but it wasn't ideal to jump to the corresponding location based on the catalog title. I use the following method to jump, but sometimes I don't get to the right place, and sometimes I get stuck or fail when the string length is too long。“mySubString(to:)” is my custom method
func scrollToPointByCatalog(string: String) {
textView.isEditable = true
let layout:NSLayoutManager = textView.layoutManager!
let container = textView.textContainer
let cutString = textView.textStorage?.string.mySubString(to: string)
let focusRingFrame:CGRect = layout.boundingRect(forGlyphRange: NSMakeRange(0, cutString!.count), in: container!)
scrollView.documentView!.scroll(NSPoint(x: 0, y:focusRingFrame.height))
textView.isEditable = false
}
Is there a good way to do that? It is a macOS software.