Scrolling to a range in a UITextView

I have UITextViewwith the text in it, and I want to be able to scroll it so that the given text is NSRangeat the top of the view. Use scrollRangeToVisible:doesn't work: it will scroll the range so that it is somewhere in the view, and not necessarily where I want. How to scroll so that this text NSRangetext is UITextViewexactly at the top?

Thanks for any suggestions.

+3
source share
1 answer

The only answer that comes to mind is to independently develop the content. Use NSString to determine the size of the window directly above the desired text (it can be difficult if the text should not start with a line, you may need to try several ranges to find one that makes the height jump). This should give you the content needed for a UIScrollView . I'm a little worried about performance, and it may be necessary to create your own implementation of UITextView in order to be effective (which should not be so difficult if it's static content). -sizeWithFont:forWidth:lineBreakMode: sizeWithFont:constrainedToSize:lineBreakMode:-setContentOffset:animated:

This is not as elegant as we would like, but UITextView does not give you much access to the layout engine.

+3
source

Source: https://habr.com/ru/post/1713696/


All Articles