I want my UITextView to scroll to a specific point (a word, or better a breakpoint: ">>>") when user presses a button outside the textview itself (on iOS). The font size can vary depending on user interaction (i.e. the user can rise or lower font size). The text itself is pasted by the user (who interactively adds the breakpoints).
I'm trying to achieve this by getting the y positions (in pixels) of the breakpoints (">>>") and then setting the offset of the textView in accordance (with setContentOffset).
The problem is that the y points I get are wrong!
Here it is a snippet of my code (for just one the ">>>" recurrences):
NSRange range = [self.textview.text rangeOfString:@">>>"];
UITextPosition *beginning = self.textview.beginningOfDocument;
UITextPosition *start = [self.textview positionFromPosition:beginning offset:range.location];
UITextPosition *end = [self.textview positionFromPosition:start offset:range.length];
UITextRange *textRange = [self.textview textRangeFromPosition:start toPosition:end];
CGRect rect = [self.textview firstRectForRange:textRange];
[self.textview setContentOffset:CGPointMake(0, rect.origin.y)];
What's wrong with my approach? I feel there's a better (and easier) way to do the same thing...
PS: I found another topic related to the same problem but it's in swift and I can't translate it: Scroll UITextView to specific text