Swift TextKit2 get substring from an NSTextRange

43 Views Asked by At

I have a UITextView and a NSTextRange. How can I get a simple String corresponding to the range?

I can only see uiTextView.text(in: UITextRange) but I am not sure if NSTextRange and UITextRange are even comparable, or how to map one to the other.

Context on why I'm trying to do this: I have some text in the UITextView and I want to figure out what portion of the text did fit the frame and which portion did not. So I do:

let textLayoutManager = textView.textLayoutManager!
let rangeOfRenderedText: NSTextRange = textLayoutManager.textLayoutFragment(for: CGPointZero)!.rangeInElement
let rangeOfRemainderText: NSTextRange = NSTextRange(location: rangeOfRenderedText.endLocation)

let stringThatFit: String = // use rangeOfRenderedText somehow
let stringThatDidNotFit: String = // use rangeOfRemainderText somehow
0

There are 0 best solutions below