IvanoBilenchi/ICTextView

How scroll to position?

DimonDeveloper opened this issue · 9 comments

In 1.1.0 version setContentOffset for this, but in 2.0 version it's not working, always set 0 for y, tested in iOS 9.
I would use the old version, but they do not work correctly in the iOS 9.

How set Y in viewDidload?
Please tell me.

I just reproduced the issue you're experiencing, and found a possible fix.

In ICTextView.m, line 196: change it to shouldApplyCharacterRangeAtPointFix = YES;

Let me know if it works okay after that change. I probably need to reapply that fix in iOS 9.x, even though it wasn't needed anymore in iOS 8.

Also, I just noticed you opened issue #10 long ago. It is referring to the same problem, right?

Yes #10 it's the same problem and shouldApplyCharacterRangeAtPointFix = YES; didn't help.
Sorry that made a duplicate question, the old you just did not respond.

That's strange, that works in the sample project.

To be clear: in the sample app, if the fix is disabled, calling setContentOffset: "very early" such as in viewDidLoad has no effect, while it works okay if the fix is enabled.

I may need some sample code. Can you setup a project where the problem you're referring to is reproduced? Thanks!

TextViewSample.zip
I created the sample project.

  1. Click "Open text" button
  2. Scroll down and remember position
  3. Click "Close" button (position remain to NSUserDefaults)
  4. Click "Open text" button again and the previously stored position does not set. (in viewDidLoad position from NSUserDefaults set in setContentOffset)

How do you think, what is the problem?

I've been pretty busy over the past few days, should have some time to look into this in a few more days. Thanks a lot for the sample project!

I had a look at your project, and it looks like the issue you're describing also happens if you replace ICTextView with a default UITextView, as can be seen in this sample project:

TextViewSample_UITextView.zip

I don't currently have much time at hand to troubleshoot this, but it's probably due to UITextView not being fully laid out during viewDidLoad. You may have some luck experimenting with viewWillAppear:, viewDidAppear:, viewWillLayoutSubviews and the likes.

Something that does indeed work is saving the location of the currently visible range, and restore it on viewDidLoad. This completely bypasses setContentOffset: by using some useful API I built into ICTextView, namely the visibleRangeConsideringInsets: and scrollRangeToVisible:consideringInsets: methods, as shown in the following sample project:

TextViewSample_Fixed.zip

It won't scroll to exactly the same point, but the text you were previously reading will definitely be on top.

@Exile90 Thank you very much, your fix helped.
In scrollRangeToVisible, if to increase "location" on 12, it shows almost the same point.

CGFloat location = [[NSUserDefaults standardUserDefaults] floatForKey:@"textLocation"]; [TextView scrollRangeToVisible:NSMakeRange(location + 12, 0) consideringInsets:YES];

I'm glad that helped. I hope you'll find the best implementation for your use case.

I'll close this issue and #10. Feel free to open a new issue if you find any other bugs, or keep commenting here if there's more to this specific issue.