Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even though this has been already answered, I had the same problem while building my own <code>UITextView</code> subclass with search highlighting (it's available <a href="https://github.com/Exile90/ICTextView" rel="nofollow noreferrer">on my GitHub</a>, if you're interested) and came up with a custom implementation of the <code>scrollRangeToVisible:</code> method. All you need to do is to adjust the <code>contentInset</code> and <code>scrollIndicatorInset</code> properties of your <code>UITextView</code> as you're already doing (<a href="https://stackoverflow.com/a/18585788/1527221">related answer</a> for casual Googlers reading this), then call:</p> <pre><code>[textView scrollRangeToVisible:range consideringInsets:YES]; </code></pre> <p>I wrapped up the relevant code in a category, which also has a couple other useful methods to account for insets in iOS 7:</p> <p><strong>Note:</strong> you need them all due to how I organized this code in my subclass. Feel free to reorganize it to your liking.</p> <pre><code>@interface UITextView (insets) // Scrolls to visible range, eventually considering insets - (void)scrollRangeToVisible:(NSRange)range consideringInsets:(BOOL)considerInsets; // Scrolls to visible rect, eventually considering insets - (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated consideringInsets:(BOOL)considerInsets; // Returns visible rect, eventually considering insets - (CGRect)visibleRectConsideringInsets:(BOOL)considerInsets; @end @implementation UITextView (insets) // Scrolls to visible range, eventually considering insets - (void)scrollRangeToVisible:(NSRange)range consideringInsets:(BOOL)considerInsets { if (considerInsets &amp;&amp; (NSFoundationVersionNumber &gt; NSFoundationVersionNumber_iOS_6_1)) { // Calculates rect for range UITextPosition *startPosition = [self positionFromPosition:self.beginningOfDocument offset:range.location]; UITextPosition *endPosition = [self positionFromPosition:startPosition offset:range.length]; UITextRange *textRange = [self textRangeFromPosition:startPosition toPosition:endPosition]; CGRect rect = [self firstRectForRange:textRange]; // Scrolls to visible rect [self scrollRectToVisible:rect animated:YES consideringInsets:YES]; } else [self scrollRangeToVisible:range]; } // Scrolls to visible rect, eventually considering insets - (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated consideringInsets:(BOOL)considerInsets { if (considerInsets &amp;&amp; (NSFoundationVersionNumber &gt; NSFoundationVersionNumber_iOS_6_1)) { // Gets bounds and calculates visible rect CGRect bounds = self.bounds; UIEdgeInsets contentInset = self.contentInset; CGRect visibleRect = [self visibleRectConsideringInsets:YES]; // Do not scroll if rect is on screen if (!CGRectContainsRect(visibleRect, rect)) { CGPoint contentOffset = self.contentOffset; // Calculates new contentOffset if (rect.origin.y &lt; visibleRect.origin.y) // rect precedes bounds, scroll up contentOffset.y = rect.origin.y - contentInset.top; else // rect follows bounds, scroll down contentOffset.y = rect.origin.y + contentInset.bottom + rect.size.height - bounds.size.height; [self setContentOffset:contentOffset animated:animated]; } } else [self scrollRectToVisible:rect animated:animated]; } // Returns visible rect, eventually considering insets - (CGRect)visibleRectConsideringInsets:(BOOL)considerInsets { CGRect bounds = self.bounds; if (considerInsets) { UIEdgeInsets contentInset = self.contentInset; CGRect visibleRect = self.bounds; visibleRect.origin.x += contentInset.left; visibleRect.origin.y += contentInset.top; visibleRect.size.width -= (contentInset.left + contentInset.right); visibleRect.size.height -= (contentInset.top + contentInset.bottom); return visibleRect; } return bounds; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload