user4809833
user4809833

Reputation:

How to detect that user is typing a message?

For example, in chat apps when a user is composing a message, app shows typing icon, but if the user stopped composing in the middle of the message, app disappears composing icon.

How the app can detects that user stopped typing in the middle of composing or still typing?

For example:

hel (show typing icon)

stopped typing (hide typing icon)

lo (continue typing - show again)

Upvotes: 1

Views: 2206

Answers (2)

Kemal Can Kaynak
Kemal Can Kaynak

Reputation: 1658

First, you need to detect when user start typing. You can addTarget your UITextField object. When user stop typing, just set a timer in that target like that;

textField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)

func textFieldDidChange(textField: UITextField) {

   textTimer?.invalidate()
   textTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("textFieldStopChanging:"), userInfo: nil, repeats: false)
   print("Start typing")
}

func textFieldStopEditing(sender: NSTimer) {

   print("Stop typing")
}

Upvotes: 3

Vivek Shah
Vivek Shah

Reputation: 420

If you are using UITextField below method may help you.

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
}

If you are using UITextView try below

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
 }

Upvotes: 0

Related Questions