0

Is there any way to detect cursor blinking in swift 4?

Actually, I wanna call the specific function after the user finished their editing, not while editing. i have used the function textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {}, func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {} and all related functionalities. Those functionalities are called when the user typed 1 word but actually, I want to call the function after they typed the full words or chars they want.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Rohini G
  • 3
  • 4
  • here in Objective-c https://stackoverflow.com/questions/7010547/uitextfield-text-change-event use notification. Or simply call didEndEdting delegate of textfield. You can detect edit end of your textfield. – Gagan_iOS Apr 26 '18 at 08:32
  • without any button clicked I wanna know the words they entered. didEndEdting will call after the done button clicked. – Rohini G Apr 26 '18 at 08:40
  • @RohiniG - you get the exact OP from shouldChangeCharactersIn – Anbu.Karthik Apr 26 '18 at 09:43
  • yup but not the exact one. please reconsider my need. – Rohini G Apr 26 '18 at 10:32

1 Answers1

0

Set a timer in shouldChangeCharactersInRange. When that fires, you'll know that there hasn't been any text input for whatever timeout value you choose:

private var timer: Timer?

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
   self.timer?.invalidate()
   self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
       // this is called 1 second after the last entry in the textfield
   }
   return true
}
Gereon
  • 17,258
  • 4
  • 42
  • 73
  • is there any chance to get the user's input without any time delay? because say, if they type 4 chars means it won't take 1sec. – Rohini G Apr 26 '18 at 08:55
  • Without the delay, you won't know if the user has actually stopped typing, and might as well use `shouldChangeCharactersIn` directly. If 1 sec seems to long, you're free to change it to smaller values. – Gereon Apr 26 '18 at 09:00
  • okay. Nope, I won't call shouldChangeCharactersIn because I need the complete word. so, i took that from the _ textField. your answer is seems to be very useful. Thank you so much. +1. – Rohini G Apr 26 '18 at 09:04
  • you have any idea about autocompletetextview as a solution for this task. autocomplete won't call during typing it will call after finished the typing without any delay. – Rohini G Apr 26 '18 at 09:12