简体   繁体   English

在 UITextView 中获取当前输入的单词

[英]Get currently typed word in UITextView

I'm trying to make a "tagging window" much like the one used in Facebook where you type "@" and it make suggestions among your friends on which one to tag.我正在尝试制作一个“标记窗口”,就像在 Facebook 中使用的那个窗口一样,您可以在其中键入“@”,然后它会在您的朋友之间就标记哪个窗口提出建议。 I'd like this feature in my app, but I can't for the life of me figure out how to get the currently typed word in order to filter suggestions.我希望在我的应用程序中使用此功能,但我终生无法弄清楚如何获取当前输入的单词以过滤建议。

I use an UITextView and I've been looking at this post https://stackoverflow.com/a/27380612/4148782我使用UITextView ,我一直在看这篇文章https://stackoverflow.com/a/27380612/4148782

but I have issues translating this to Swift 3, and even so the comments suggests that this isn't solved anyway.但是我在将其转换为 Swift 3 时遇到了问题,即便如此,评论表明这无论如何都没有解决。

So the functionality I'm after is:所以我追求的功能是:

  • User starts typing in a UITextView and if the word begins with a "@" I'd like to extract the word.用户开始在UITextView输入,如果单词以“@”开头,我想提取该单词。
  • I'd like to replace the word with a certain input as well.我也想用某个输入替换这个词。 Let's say the user types @abc and I filter a suggestion which states " abcdef ", then I want to be able to replace the @abc in the UITextView with " abcdef ".假设用户键入@abc并且我过滤了一个说明“ abcdef ”的建议,然后我希望能够用“ abcdef ”替换 UITextView 中的@abc

Note that I want the word currently getting typed and not the most recent typed word.请注意,我想要当前输入的单词而不是最近输入的单词。

This works for me.这对我有用。

extension UITextView {

var currentWord : String? {

    let beginning = beginningOfDocument

    if let start = position(from: beginning, offset: selectedRange.location),
        let end = position(from: start, offset: selectedRange.length) {

        let textRange = tokenizer.rangeEnclosingPosition(end, with: .word, inDirection: 1)

        if let textRange = textRange {
            return text(in: textRange)
        }
    }
    return nil
   }
}

As usual I spent a fair amount of time trying to get this to work, and minutes after I posted the question I got it working.像往常一样,我花了相当多的时间试图让它起作用,在我发布问题几分钟后,我就让它起作用了。 So here's the code I used, but I have deprecated call for the let myRange所以这是我使用的代码,但我不赞成调用let myRange

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

    let myRange = Range<String.Index>(start: textView.text.startIndex, end: textView.text.startIndex.advancedBy(range.location))
    var subString = textView.text.substringWithRange(myRange)
    subString += text

    let wordArray = subString.componentsSeparatedByString(" ")
    if let wordTyped = wordArray.last {
        currentWord = wordTyped
        print("word typed: " + wordTyped)
    }

    return true
}

Swift 4:斯威夫特 4:

This solution worked for me inside the method func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool of UITextViewDelegate.这个解决方案在方法func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool of UITextViewDelegate 中对我func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool

  func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        guard let textFieldText = textView.text,
              let rangeOfTextToReplace = Range(range, in: textFieldText) else {
                return false
        }

        let currentText = textFieldText.replacingCharacters(in: rangeOfTextToReplace, with: text)
    
        return count <= 150
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM