on clicking a text view, always positioning cursor at the beginning - ios

I have a text view. I have not implemented the UITextView Delegate Protocol in my .h, but in .m file I am overriding the delegate methods (shouldBeginEditing and endEditing) and the methods are getting called. Now, what I want to do is whenever user clicks on text view, I always want to position the cursor at the beginning (no matter where he clicks on the text view). I have tried using this lines in textview's BeginEditing method, but it was of no use.
[textView setSelectedRange:NSMakeRange(0, 0)];
I always want to allow user to enter new text at the beginning (don't want the text at the end of already entered text view's text). Someone pls tell me how to get rid of this problem?
PS: I working on ios , xcode 4.2....

UITextView*textView;
[textView setContentOffset:CGPointMake(0, 0) animated:YES];

Related

UITextView Will Not Scroll to Top

Update - I now believe this is due to me calling [view.details scrollRangeToVisible:NSMakeRange(0, 1)]; too early--meaning before the constrains have been applied and layouts recalculated.
I figured this much out by calling the scrollRangeToVisible after the view was shown and after I took some action on the cell. (This would guarantee that all layout changes were complete before I made that call.)
Thus, what I need to know now is Where should I make this call? such that it occurs after the layouts?
Original Post
I've tried many solutions, but none of them seem to work.
I have a UITextView as a subview to a UIView -- a container for the text view plus a few other items. When a user clicks on a cell in a tableView, I pop up the UIView container to show more details--the full text.
Configuring the view, I set the textView.text value to my long string, I update the font, I set the color, and I show the container view. Unfortunately, it shows the text at the bottom of the textview rather than at the top.
I've tried the following solutions, to no avail--each one results in the same behavior as above: it still shows the text at the bottom of the textview.
Attempt 1: scrollRangeToVisible
[self.textView scrollRangeToVisible:NSMakeRange(0,1)];
Attempt 2: contentOffset (tried each of the following, but none work)
[self.textView setContentOffset:
CGPointMake(0, -self.textView.contentInset.top) animated:YES];
[self.textView setContentOffset:CGPointMake(self.textView.contentOffset.x, 0)
animated:YES];
[self.textView setContentOffset:CGPointMake(0, self.textView.contentSize.height-textView.frame.size.height) animated:YES];
Am I fishing with those last items? Yes. I've run out of ideas. I've checked the values of contentOffset.y and contentInset.y -- both are zero.
I create the UITextView in a subclass of UIView called PrayerPopupView. Here's the process:
In my main UIViewController where the table exists, a user touches a cell. Based on this action...
I create an instance of PrayerPopupView in this view controller
In the creation of this view, the UITextView is created
The textView.text value is set.
I call `[self.details scrollRangeToVisible:NSMakeRange(0, 1)];
I use a custom class (KLCPopup) to add the view to the UIWindow but I also tested by adding my PrayerPopupView directly as a subview of the view controller rather than using KLCPopup, just in case it mattered. Both yield the same result of text not scrolled to the top correctly.
Since adding the scrollRangeToVisible didn't work in the custom view itself, I also added it in the UIViewController before showing the PrayerPopupView ([prayerPopupView.detailsTextView scrollRangeToVisible:NSMakeRange(0, 1)];), and still the same result.
Here's a screenshot of the view:
Here's a screenshot of the view properties in the debugger:
This shows the view hierarchy in the debugger:

manipulating the keyboard: change frame, edit text field other than the one tapped

I'm having trouble finding info on manipulating the keyboard. Here's what I want to do:
When the user touches a text field, I want a keyboard to appear. However:
1) I want to move the keyboard up 30 pixels so that another view is visible south of it.
2) The keyboard should not actually edit the text field the user touched. A multiline text view will be presented for the user to edit instead.
I'm not sure of the best strategy and I don't know the methods I'll need to use or properties I'll need to set. Some sample code would be greatly appreciated.
As #HenryHarris says, you can't move the keyboard (or more appropriately shouldn't, as Apple expects it to be where they put it and will most likely reject your app if you start messing with it).
You can on the other hand, switch to a different view when they tap the text field by using something like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Present your text view, let's call it myTextView for now, and then tell it to start accepting input:
[myTextView becomeFirstResponder];
// Now return NO so that the textField does NOT become the first responder:
return NO;
}

iOS Autocorrect in Textview

In Apple's Messages app, if you make a typo, the little correction bubble shows up. Before you hit space, this bubble lets you know that any further activity (spacebar or submit button) without dismissing the bubble will result in the autocorrection.
I have a text submission form in my app - but in my app, when the user tries to submit the textview while the autocorrect bubble is up, the textview will NOT take the autocorrection. How do I make it take the autocorrection and THEN submit?
Screenshot of the autocorrect bubble.
http://imgur.com/tLyJn
You need to call [self.view endEditing:YES] when you "submit" the text view where self is the view controller containing the text view.
The call to endEditing: will ensure the proper chain of events occur resulting in the text view's text being updated properly. This is what I do in my app.

resignfirstresponder on a UITextView inside a UITableViewCell

I am hoping if someone can help me resolve an IOS/XCode question.
I need to have a UITextView created inside a UITableViewCell, this UITextView has responds to a user click, upon which a UIPopoverController will be displayed so that a sub-UITableView is displayed (inside the UIPopoverController) allowing a user to select from a list of choices (lines of text). After the user select the choice (one of the line of text), that line of text will then be displayed inside the said UITextView. First problem I am having is that when the user click on the UITextView the keyboard gets displayed instead of the UIPopoverController. How do I go about disabling ie. calling resignFirstResponder so that instead of the keyboard displaying, I get the UIPopoverController coming up instead. Would someone be kind enough to share similar codes? or point me to some sample of how this can be done? Thanks so much in advance.
You can use following delegate method to detect when textView is tapped and show your popOverController accordingly, return 'NO' in the delegate method so that no keyboard will appear...
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
// code to show popOverController
return NO;
}

Programmatically show the keyboard on iPhone app

I have a view with only one UITextView that is used to enter a value. I want that when the view shows, the textview becomes the first responder (that's the easy part) and also the default keyboard shows up. I tried searching for this in speca but to no avail. There are many posts on how to dismiss the keyboard, but what I want is to show the keyboard w/o waiting for user to touch my textview.
Just setting the text field to firstResponder should do the trick.
Inside your -viewDidLoad:
[myTextField becomeFirstResponder];

Resources