Detecting when a user is beginning to edit an UITextView - ios

I am wondering if it is possible to detect if a user is beginning to edit a text view (so that I can make certain adjustments to the view). Would I have to detect for the keyboard instead?

Yes it is possible using UITextView's delegate. You can see documentation of the UITextViewDelegate protocol here.
There is a nice tutorial found here that will show you how to detect these interactions using the UITextViewDelegate protocol.

Take a look at UITextViewDelegate. Implement textView:shouldChangeTextInRange:replacementText: if you want to react while the user is typing or textViewDidChange: otherwise.
Edit: If you are only interested when the text fields becomes the first responder (i.e. the user sets the focus), use textViewDidBeginEditing.

Assign your textView's delegate in your xib file (or programmatically)
Use one of these:
- (BOOL)textViewDidBeginEditing:(UITextView *)textView {
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {

Related

HKTextview emoji detection

I'm having an issue. I am using the Hakawai framework in an app so that I can have mention support (#username).
The issue I've run into is that the textfield I am using is not registering the case where there is no text and a user types an emoji into the textview. As we are using HKWTextView, I believe the textViewShouldChangeTextInRange delegate method is never called, even if implemented. The only replacement I can think to use is :
- (void)textView:(HKWTextView *)textView didChangeAttributedTextTo:(NSAttributedString *)newText
originalText:(NSAttributedString *)originalText
originalRange:(NSRange)originalRange;
in HKWTextView, but that's still not picking up on emojis being typed in when no other text has.
The functionality I would like is:
- Text view is empty
- user types in anything, emoji included
- textview width shortens, "Post" button appears.
Right now, typing emojis into the empty text view will not make the post button appear. However, it's worth mentioning that once the emojis are typed in, if there is more than one, deleting one of them WILL make the post button appear. I'm at a bit of a loss here.
I found the answer to this - It turns out that HKWTextView does some rewiring of the UITextView delegate methods that are fired. Try handling the input in the UITextView delegate method textViewDidChangeSelection. That method will be fired when an emoji is typed.

iOS 8 Keyboard Extension Detecting first responder change

I'm writing a keyboard extension for iOS (hence overriding UIInputViewController) and I'm trying to figure out how to detect when the first responder changes. Is this even possible?
My motivation is that when the user selects a different text input field (while the keyboard is active) the style of the keyboard might need to change to suit the attributes of that input. This can happen when there are several text fields displayed on a UI and the user first selects one (causing the keyboard to be initialized) then the user selects another with different attributes (keyboard doesn't know it).
I've looked through the methods exposed by UIInputViewController and the delegates it implements but nothing I've seen really fits the bill. The closest thing I've found is selectionDidChange on UITextInputDelegate.
I found the best way to get this information is to override the UITextInputDelegate textDidChange method (UIInputViewController implements UITextInputDelegate). It turns out that textDidChange is called whenever the user switches the input text field (first responder), or when the text changes for some reason (luckily not when it is your keyboard that initiated the change).
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
That should tell you when it expects to become firstResponder. A couple things to keep in mind;
*This will only be called when a UITextFied is the thing becoming firstResponder. If some other object decides to, this won't be called. It'll probably call the method below.
-(BOOL)becomeFirstResponder
*Your class must conform to the UITextFieldDelegate and all of your UITextFields must set their delegates to self. Without that they won't call shouldBeginEditing on your class.

Using tableView:didDeselectRowAtIndexPath: to edit a UITextField in a cell

How would you use tableView:didDeselectRowAtIndexPath: to trigger textFieldShouldBeginEditing: ?
Or what other options do I have? I did consider having tableView:didDeselectRowAtIndexPath: trigger a UIAlertView (with textField) but it isn't aesthetically pleasing.
Your question is a little light on details: what's the user interface look like? What are you tying to accomplish?
To literally answer your question, you can just have didDeselectRowAtIndexPath implementation call the method textFieldShouldBeginEditing - however I don't believe that's what you're asking? What it sounds like you're asking is how you can get a touch event on a table view cell to pass into a UITextField so that editing begins.
That would make this a duplicate of other questions already asked here:
Having a UITextField in a UITableViewCell
Not sure how proper the practice is, but I have an app I use a textField that is 1x1 points and off screen. If I want to bring up the keyboard, I call
[self.myTextField becomeFirstResponder];
Then you can use the UITextFieldDelegate methods to control and update information being entered on the keyboard.
Then when the user is done, I call
[self.myTextField resignFirstResponder];
Of course this is assuming that you don't actually want to use a textField in your tableView.
Hope this helps.

textViewDidBeginEditing: within UIWebView not being called

i´ve created a subclass from UIView and put in a UIWebView. This displays a simple external website which contains just two textfields. The user can type in his email address and the verification.
I´ve already included the UIWebViewDelegate for the webViewDidFinishLoad: method. Additionally i´ve created a NSNotification for "UIKeyboardDidShowNotification".
Everything works, but when i select the second textfield, the first textfield automatically will be scrolled half out of the visible area. I´ve also included the UITextViewDelegate with - (void)textFieldDidBeginEditing:(UITextField *)textField but i get no NSLog from that delegate method.
Any help how i can get the frame of the selected textfield (within a UIWebView)?
Thanks for any hints...
I´ve read that there are no UITextField delegates when using UIWebView. So therefore no information about which textfield is currently selected, right?

custom input view keyboard functionality

I wanted to ask a quick question just to make sure I am not missing anything simple before I implement a more difficult method. I need to create a custom keyboard for an iPhone application. This I have already done by creating a view with the buttons, using a custom input view and it displays exactly like it should. Now most of the buttons are standard numbers which need to update a UITextField in the screen that called the keyboard. Does anyone know a simple way to do this? I assume there has to be a built in function that the keyboard uses to send the information but I haven't been able to find any reference to it. Otherwise I will have to go the more difficult route. If anyone has a simple way to do this I would appreciate it. I haven't worked with custom keyboards before.
You won't be able to do it the same way that Apple does it, as their keyboard is basically an input device, globally.
I recommend you just append the data in your button press multiplex method. Here's an example:
NSString *appendThisText = #"subtitle";
self.myTextView.text=[NSString stringWithFormat:#"%#%#", self.myTextView.text, appendThisText];
Custom keyboards are simpler than you realise.
UITextField conforms to the UITextInput protocol. That's a bit of a red-herring because this protocol provides all the really complex stuff like selecting text and so on. But UITextInput itself conforms to UIKeyInput. This is your friend.
The key UIKeyInput methods are:
- (void)insertText:(NSString *)text;
- (void)deleteBackward;
Your keyboard class should have a delegate (which points to the textfield that the keyboard is operating on) and you simply call these methods to insert and delete text.

Resources