Swift TextKit2 получает подстроку из NSTextRange ⇐ IOS
-
Anonymous
Swift TextKit2 получает подстроку из NSTextRange
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
Источник: https://stackoverflow.com/questions/780 ... stextrange
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
Источник: https://stackoverflow.com/questions/780 ... stextrange
Мобильная версия