[英]Get the last word that is being typed
I want to get the word that is currently being typed in a UITextField. 我想获取当前在UITextField中键入的单词。
Case 1: 情况1:
hello there 你好
If the cursor is after the second e (meaning e has just been typed, then the word there should be returned 如果光标在第二个e之后 (意味着刚刚输入了e ,则应该返回那里的单词)
Case 2: 情况2:
User deletes o from hello (cursor is after the second l ), then the word hell should be returned 用户从hello中删除o (光标在第二个l之后 ),然后应返回单词hell。
I have some code for this but it is returning all text in the UITextField. 我有一些代码,但是它返回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. 更新1:我经历了这些类似的问题,但是这些问题对我没有用。
Get currently typed word in UITextView 在UITextView中获取当前键入的单词
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.