0

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.

  • Is this what you're trying to do? [Find a substring within a NSTextView and then scroll to the substring's location](https://stackoverflow.com/questions/49927964/find-a-substring-within-a-nstextview-and-then-scroll-to-the-substrings-location) – Willeke Jan 11 '22 at 10:35
  • Willeke, It didn't help me solve the problem, using it the same way I did before, when the data was too large, it caused failure or lag. – cccToTree Jan 12 '22 at 02:54
  • What are you trying to accomplish, can you give an example? How is the text view configured? Post a [mre] please. – Willeke Jan 12 '22 at 05:15

0 Answers0