I have a UITextView, to which I am adding some Custom UI. This custom UI is laid out based on the position of the text inside the UITextView.
I am using the firstRectForRange
method (from UITextInput) on the UITextView. This method returns the frame of the specified text range in the textView.
However, it does not work correctly right after setting the attributedText. It takes a minimal amount of time before it will return the expected value. This period could very likely be the time until the first frame with the updated text is rendered and displayed on the screen.
I am currently wrapping the code that lays out the custom UI into a dispatch_async(dispatch_get_main_queue(), ...)
block, which delays its execution until after the frame is rendered (by about 9ms).
What does UITextView do inbetween setting the attributedString and displaying the next frame on the screen?
Is there a better place / method call for me to hook into?
Is there any way for me to force running that action synchronously?
Things I have tried calling:
(after setting attributedText; before calling firstRectForRange)
- Calling
setNeedsLayout
andlayoutIfNeeded
beforefirstRectForRange
. - Calling
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)