I am continuously appending text to the content of my UITextView and would like to auto-scroll to the latest update, only if if the UITextView has already been scrolled to the bottom. Any ideas on how this could be implemented?
UITextView inherits from UIScrollView, so you can therefore access all of the same properties as with a UIScrollView. Personally, I would use a combination of
myScrollView.contentOffset.y
to detect whether or not the text is scrolled all the way down, and
[myScrollView zoomToRect:CGRectMake(x,y,height,width) animated:YES]
to shift the current view to the newly added text.
See the docs for UITextView and UIScrollView. If you didn't know already, those can be accessed from XCode by clicking Help>>Developer Documentation
Good Luck!
James
To all those who want to automatically scroll a uitextview when a keyboard appears/disappears, search and download keyboardaccessory.xcodeproj by apple.
Although the program tests in an ipad simulator, you can copy the parts of the code and paste them in yours.
Specifically:
Copy the code on listening for the 2 notifications for keyboardWillShow and keyboardWillHide in the viewDidLoad.
Copy the code releasing the notification in dealloc.
Remove notification listening status in viewDidDisappear.
Copy the codes in keyboardWillShow, keyboardWillHide to programmatically compute for the height of the keyboard and scroll the text automatically.
Remove the keyboard in textViewShouldEndEditing by calling [textView resignFirstResponder];
If you want this implemented in both portrait and landscape, the code above will already do the necessary computations. all you have to do is allow rotation in shouldRotateToInterfaceOrientation
Related
The problem I am encountering now is that there is a UITextView component in UIScrollView and that UITextView component will interfere the scrolling of UIScrollView, i.e. when a user's finger is on that UITextView to scroll the whole UIScrollView, the screen won't scroll. The content in the UITextView is dynamic, i.e. the size of UITextView component is dynamic. The worst case is the whole screen is occupied by the UITextView and users won't be able to scroll the screen at all.
The UITextView is disabled for editing. I choose UITextView instead of UILabel because I want to detect phone number & link in the supplied text and utilize the built-in function to invoke dialer and browser when users click the phone number or link.
Any suggestion about how I can solve this problem or any clue about why this happens are all appreciated! Thanks.
UITextView inherits from UIScroll view so it has a property scrollEnabled. Just put it to false!
If you are using a storyboard or nib file, select your UITextView and uncheck Scrolling Enabled in Interface Builder.
Otherwise, do this:
textView.scrollEnabled = NO;
Apart from the property scrollEnabled, you also have the property userInterationEnabled that disable(if you set it to FALSE/NO) any touch events on the view (scrolling included, of course). Hope it helps, specially for other situations cos in your case it makes the same effect as disabling the scroll.
I have a strange issue , I have a page app in which one page I had used a UITextView, but when I tap on the UITextView it moves to the next page, can't write anything on it.
There is no issue when I use UIPageViewControllerTransitionStylePageCurl. The only issue is with UIPageViewControllerTransitionStyleScroll. Anyone know the solution? please help me
I ran into the same issue myself, and it appears that it's a consequence of some default behavior that occurs whenever a UITextField or UITextView becomes first responder inside a UIScrollView.
The solution to prevent your UITextView from scrolling the UIPageViewController is to first embed it into a UIScrollView instead of directly into a page's main view.
This works because when the UITextView becomes first responder it will search for the earliest superview in the view hierarchy that is of type UIScrollView, and it will scroll that scrollview to accommodate for the keyboard covering the screen. If you don't wrap the UITextView inside a UIScrollView, your UIPageViewController's internal UIScrollView will be chosen and scrolled, producing undesired behavior.
You can refer the link below..
UIPageViewControllerTransitionStylePageCurl - http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPageViewControllerClassReferenceClassRef/UIPageViewControllerClassReference.html
UIPageViewControllerTransitionStyleScroll - http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPageViewControllerClassReferenceClassRef/UIPageViewControllerClassReference.html
Am working in message based iPhone application. In my application looking like iMessage native iOS app. I made Bubbles with used UIImageView and UILabel. I made UILabel as clickable and showing Copy option. It is working fine when the message input UITextView is not in active.
1. I can show the "Copy" option when we clicking UILabel and the UITextView is not becomeFirstResponder.
2. When the user clicking the MessageTextView (UITextView) from the bottom of the screen the UITextView becoming first responder and keyboard is showing now. In this scenario if the user clicking the messabe bubble (UILabel) the UIMenuItem showing "Paste" on the bubble instead of "Copy".
3. If i click "Paste" from the bubble UIMenuItem already copied text will be pasting in UITextView. So the control fully in UITextView UIMenuController not activated in UILabel. So i cleared the text from UIPateBoard when the user clicking the Bubble (UILabel).
4. Now the UIMenuController not showing up even [self becomeFirstResponder]; not becoming in UILabel class.
The reason is when the UITextView is in becomeFirstResponder the control fully in that. not coming to UILabel. Could you please help me on this.
How to show UIMenuItem "Copy" when the user clicking UILabel if the keyboard is in visible the control is in UITextView? Could you please help me on this. I spent two days in this issue. Thanks in advance.
May be I am wrong.CONSIDER THIS ANSWER AS A COMMENT.
I tried to achieve like what you are trying to do. Thats not working too. I figured out I cannot make access the two views at the same time. Especially when I have any view becomeFirstResponder and you cannot access menu items of other view.
But if you try like this,you may succeed in your code.
1) In the touchesBegan: method, find the user touching inside your UILabel.
2) If that happens, then show a custom view with buttons like copy,paste and select like that.
The latest version of Apple's UICatalog example application includes zero code in the TextFieldController for handling keyboard show/hide events, and yet the table view still slides up and down beautifully with the keyboard.
Does anyone know what the new trick is? Are there settings in the XIB that allowed them to forgo registering for the notifications or using TextField delegate methods?
The TextViewController still uses keyboard notifications to deal with view sliding, so I'm really confused as to why this isn't included for TextFields anymore.
Thoughts?
You can close the keyboard, if it's open by calling:
[sender resignFirstResponder];
Not sure about opening the keyboard however.
The trick is hidden within calling becomeFirstResponder on a UITextField that is in a scrollable view. Apparently, whenever calling [textField becomeFirstResponder], iOS automatically scrolls the parent view until said textField is visible.
This behavior can actually be undesirable in some cases, as it will not usually scroll to the same location that the UIScrollView method scrollRectToVisible:animated: would if you were to try to do things that way.
Thanks for your thoughts everyone!
I have a UITextView that is properly displaying URLs thusly:
contentView.editable = NO;
contentView.dataDetectorTypes = UIDataDetectorTypeAll;
My goal is to make it so you can still tap on this text view in order to edit its text at a particular location (just like the built-in Notes app). That way, if you tap a link, it'll launch a browser, but if you tap anywhere else, it'll start editing at the point where you tapped. Should be easy, right?
Not so far. Subclassing the UITextView and overriding touchesEnded gives you a chance to set editable to YES. But when you do that, the text view doesn't remember where you tapped (the selectedRange doesn't get set properly), so editing always begins at the bottom of the text view.
I've even tried using the undocumented setSelectionWithPoint method, but it doesn't behave as you'd expect.
Can anyone think of some way to achieve a proper tap-to-edit UITextView with tappable links?
Perhaps you could try either retaining the UITouch and UIEvent or forging copies using custom classes that have the same class signatures, then re-send the touchesBegan: and touchesEnded: events after setting editable to YES.