1

I want to get the word that is currently being typed in a UITextField.

Case 1:

hello there

If the cursor is after the second e (meaning e has just been typed, then the word there should be returned

Case 2:

User deletes o from hello (cursor is after the second l), then the word hell should be returned

I have some code for this but it is returning all text in the UITextField.

postView.textView.delegate = self

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let text = (textView.text as NSString?)?.replacingCharacters(in: range, with: text)

    return true
}

Update 1: I have gone through these similar questions but these didn't work for me.

Get currently typed word in UITextView

Get word that is being typed

Hemang
  • 26,840
  • 19
  • 119
  • 186
Mujeeb
  • 995
  • 1
  • 8
  • 18
  • You want a word or valid word to be returned. As i understand you want a valid word from a sentence. eg "wertuwerg hjfhdsf" these words should not be returned but "Hello There" should be returned ?? – Chanchal Warde Jun 06 '17 at 04:29
  • @ChanWarde I need the word, regardless of whether it is valid or not. But it needs to be the word that is being typed (most recent), **not** the word that's the last word in the UITextView. – Mujeeb Jun 06 '17 at 04:34
  • let newString = NSString(format:"%@", textView.text).replacingCharacters(in: range, with: text) as NSString let textNew = newString.substring(to: range.location).components(separatedBy: " ").last try this and update it also as its getting word you are typing but till you typed.. – Chanchal Warde Jun 06 '17 at 05:43

1 Answers1

4

Try with below code, its working at my end.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let nsString = textView.text as NSString?
    let newString = nsString?.replacingCharacters(in: range, with: text)
    let arr = newString?.components(separatedBy: " ")
    self.lblWord.text = arr?.last
    return true
}
Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
  • What is `self.lblWord.text` for? – Mujeeb Jun 06 '17 at 05:38
  • you can remove that line. It will give you last word from the string. It was for me while testing for you... – Jigar Tarsariya Jun 06 '17 at 05:50
  • here `arr?.last` will return you last word which you want. so you can get that word from here – Jigar Tarsariya Jun 06 '17 at 05:52
  • How can I get this last word? Do I need to store it in `self`? – Mujeeb Jun 06 '17 at 05:54
  • I have declare one label like `@IBOutlet var lblWord : UILabel!`. so i am displaying that last word in this label. so you can can store this last word in any controller where you want. – Jigar Tarsariya Jun 06 '17 at 06:09
  • Got it. I just created a new variable in my class like this `var lastWord = ""`, then replaced your line with `self.lastWord = (arr?.last)!` – Mujeeb Jun 06 '17 at 07:49
  • Why is this an accepted answer? The code doesn't work, it just return the last word in text but not the last word that is being typed (when cursor is moved to the middle of the text and user types there) – Lavrin Pristazh Jun 15 '20 at 09:24