Scroll back to top in iOS UITextView after change of text - ios

I have a UITextView. The text in the view keeps changing based on user action. On many occasions, the text is too large to fit and so my text view (which has a desired fixed width and height) scrolls the text. The control flow is something like this
1) user takes some action, and text view appears with some text (possibly more than what can fit, so comes with a scroll)
2) user dismisses the view (I simply hide the text view)
3) user another action, and text view appears with new text (i use UITextView setText to reassign the text, and simply turn the hidden property to NO)
Problem Scenario:
Perform step 1 above, user scroll down a little to read the content, then proceeds with step 2 and 3. But when the text view reappears, the view is still scrolled a little from the last interaction. I want the text to be visible from the start, meaning, the scroll shud be "reset"
What I tried:
[textView scrollRangeToVisible:NSMakeRange(0,0)]; //OR
[textView scrollRangeToVisible:NSMakeRange(0,600)]; //600 being an approximate number of characters that can show in my text view after which they flow out and it has to be scrolled.
this was the most successful attempt. Worked most of the time, except, sometimes, intermittently, there would be a lot of extra space on top of my line 1 of the text, like someone scrolled/swiped it down and froze it there.
PS:My text cannot be edited by the user. It is always reset to a different string in code

Related

CosmicMind Material TextField : Adjust TextField position when it becomes first responder

I have a view and I have two Material Textfields in it. The problem is when I have to show detail Message or when the I click on text field the placeholder moves up. So if the space between fields is less then the animated placeholder text gets overlapped with above text field.
So far I have kept enough space between fields to rid of this issue but it does not look good moreover, in Android when the user clicks on the textfield and its animated text shows the textfield automatically moves some points down or up to cater the inner spacing issue.
Default View with

UITextView only displays first line of text

I have a UITextview. It has enough text in it that it flows into multiple lines. However when I run the app on a device, the text does not flow onto subsequent lines, it's as if the text is carrying on beyond the bounds of the view.
All the settings for the view are the default, but user interaction is disabled. The view has constraints which bound it to the margins of the viewport. I've coloured the background of the view to check this - the view is not running off the edge of the viewport, and has sufficient height that the 2nd and 3rd lines of text should fit on the view, but below the first line of text the view is empty.
What is going on?

iOS UITextView content offset and bounds origion change after changing text programmatically

I have a UITextView. The text in the view keeps changing based on user action. On many occasions, the text is too large to fit and so my text view (which has a desired fixed width and height) scrolls the text. The control flow is something like this
1) user takes some action, and text view appears with some text (possibly more than what can fit, so comes with a scroll)
2) user dismisses the view (I simply hide the text view)
3) user takes another action, and text view appears with new text (i use UITextView setText to reassign the text, and simply turn the hidden property to NO)
My text cannot be edited by the user. It is always reset to a different string in code
Problem:
When the textview is redrawn/rendered with the new text, on some occassions the contentoffset.y AND bounds.origin.y values of the text view are set to something besides 0, which is not desirable. It is intermittent, but creates a major UI issue. At least I think those new values are what causes the UI to look so weird. Open to other suggestions here...
I want to know why this happens, and how to fix this? Since the text is changed programatically, I can't depend on textViewDidChange: to reset these values.
EDIT: This is how I am changingt he text everytime something new has to be placed in the textview
[textView1 setText:someNSString];
where someNSString is, well, some string with text

NSTextView stops being able to scroll once new text is set

I have a TextView made in the Interface Builder;
It scrolls fine with its default text, but as soon as I programatically set the text to something else, it no longer scrolls.
I do not disable scrolling, and I have tried enabling scrolling (setScrollEnabled:YES), and user interaction (setUserInteractionEnabled:YES) both of which make no difference.
The things that I have found:
It scrolls with the default text showing
It doesn't seem to matter what text I set in it, it just stops scrolling as soon as it is set. I'm doing it programatically like this:
self.myTextView.text = #"any text"; (or [self.myTextView setText:#"any text"]; has the same effect.)
I have two other Textfield in the same View, one of which is using the View Controller as its delegate. The TextField in question is not using the View Controller as its delegate. None of the delegate methods set anything to do with scrolling.
Both of the other TextViews have their scrolling capability set to defaults. If I change the one that doesn't need it to no scrolling, it makes no difference to the problem.
The one that has the View Controller as its delegate continues to scroll after I edit it (non programatically) and this one continues to be able to scroll once the one in question freezes.
If I delete these two other TextFields, then the one in question works as expected, i.e. it scrolls after adding text.
Any thoughts?
Thanks.
Make sure your new text is long enough to be able to get scroll.
if text is just one line it wont scroll but if its larger then text area size then it will scroll its by default settings

iOS: Stop ScrollView from making room when selecting textfield at top

I've scoured through several questions asking about how to prevent scrolling when selecting a first responder however nothing I find seems to work for this issue.
I have a UITextField above my table view at the very top of my page. When the user selects it (or when I do it programmatically) it drops down a bit, seemingly giving room for options such as "paste" or autocorrect (which I've disabled). I'd like to prevent this from happening.
I've tried setting the scroll position myself, which it initially does, but then it instantly jumps down a bit. Is there a way I can make the paste/edit bubble appear below the textfield (like it does with textfields in the header)? I'm thinking perhaps that will prevent it from jumping downwards, but I can't find information on this.
I'm completely stuck so any help is appreciated. Links, vague suggestions, whatever. Thank you.
Try embedding your text field in a scroll view the same size as your text field.
The reason why this should work is because when a text field becomes first responder, it only scrolls the scroll view that is its most recent ancestor in the view hierarchy. If it only finds the dummy scroll view, whose content size should not exceed its bounds size, then no scrolling should occur in either scroll view.

Resources