在textview ios中逐字逐句删除字符

时间:2018-02-19 12:35:18

标签: ios swift textview

我一直试图逐字逐句地删除这意味着如果我有一个像“Hello,这个@Sivajee是示例文本”这样的句子,每当我删除'e时 @Sivajee 中的字母应该从句子中删除整个单词 @Sivajee 。我真的不知道这个。

这是我的代码。

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

    //Tagging users
    if text == "@" || substring == "@" || (isTagging && text == " "){
        isTagging = !isTagging
        searchTerm.removeAll()
        tagSearchedUsers.removeAll()
        tableView.reloadData()
    }
    else if isTagging {
        searchTerm.append(text)

        // to limit network activity, reload half a second after last key press.
        NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(self.getFriendsSearchResults), object: nil)
        self.perform(#selector(self.getFriendsSearchResults), with: nil, afterDelay: 0.5)
    }

    return true;
}

总的来说,我的想法是在输入时标记某人&用户使用退格时删除。您的意见对我来说更有价值。

修改

来自评论的提议解决方案的快速等效

 if (string == "") {
        let selectedRange: UITextRange? = textField.selectedTextRange
        let cursorOffset: Int = textField.offset(from: 0, to: (selectedRange?.start)!)
        let text: String? = textField.text
        let substring: String? = (text as? NSString)?.substring(to: cursorOffset)
        let lastWord = substring?.components(separatedBy: " ")?.last as? String
        if lastWord?.hasPrefix("@") ?? false {
            // Delete word
            textField.text = self.textField?.text()?.replacingOccurrences(of: lastWord!, with: "")
            return false
        }
    }

2 个答案:

答案 0 :(得分:2)

斯威夫特4:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
{
    if text == ""
    {
        if let selectedRange = textView.selectedTextRange
        {
            let cursorOffset = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)
            if let myText = textView.text
            {
                let index = myText.index(myText.startIndex, offsetBy: cursorOffset)
                let substring = myText[..<index]
                if let lastword = substring.components(separatedBy: " ").last
                {
                    if lastword.hasPrefix("@")
                    {
                        //Check complete word

                        let completeLastword = myText.components(separatedBy: " ").filter{$0.contains(lastword)}.last

                        textView.text = myText.replacingOccurrences(of: completeLastword!, with: "")
                        return false
                    }
                }
            }
        }
    }

    return true
}

答案 1 :(得分:1)

以下是评论者提出的答案的 Swift 版本。

 if (text == "") {
                let selectedRange: UITextRange? = textView.selectedTextRange
                let cursorOffset: Int = textView.offset(from: textView.beginningOfDocument, to: (selectedRange?.start)!)
                let text: String? = textView.text
                let substring: String? = (text as NSString?)?.substring(to: cursorOffset)
                let lastWord = substring?.components(separatedBy: " ").last

                print(lastWord)
                if lastWord?.hasPrefix("@") ?? false {
                    // Delete word
                    textView.text = textView.text?.replacingOccurrences(of: lastWord!, with: "")
                    return false
                }
        }

但它仍然有退缩,当从中间删除单词时,它将只删除它的一部分。不完整的一句话。这意味着,如果我有 @Sivajee 字样,如果我将光标放在字母'V'和退格时,它只会删除 @Siv 。不是 ajee