Reputation: 18375
I have a UITextView
with text in it, and I want to be able to scroll it such that a given NSRange
of the text is at the top of the view. Using scrollRangeToVisible:
does not work: it will scroll the range such that it is somewhere in the view, not necessarily where I want it to be. How can I scroll such that this specific NSRange
of text in the UITextView
is exactly at the top?
Thanks for any suggestions.
Upvotes: 3
Views: 1345
Reputation: 299275
The only answer that comes to mind is to work out the contentOffset yourself. Use NSString -sizeWithFont:forWidth:lineBreakMode:
sizeWithFont:constrainedToSize:lineBreakMode:
to work out the size of the box immediately above the text you want (this may be tricky if the text doesn't have to start the line; you may have to try several ranges to find the one that causes the height to jump). That should give you the contentOffset you need for UIScrollView -setContentOffset:animated:
. I'm a little worried about the performance, and it may turn out necessary to create your own implementation of UITextView to be efficient (which shouldn't be that difficult if it's static content).
It's not as elegant a solution as I'd like, but UITextView doesn't give you a lot of access to the layout engine.
Upvotes: 3