ios - 调用 UITextViewDelegate ShouldTextChangeIn 时的奇怪行为

标签 ios swift uitableview uitextview hashtag

我有一个 UITableViewCell,我有一个评论 UITextView 来写评论。在写下评论时,我检查了 #hashtags@mentions 以检查它们在应用程序服务器端的可用性并将它们显示在 tableView.

问题是,一旦委托(delegate)方法被调用,我就无法重置它以检查其他 if 语句。例如 "" 空格字符 "如果我以 #hashtag"开始我的文本,则永远不会调用 If 语句,所以我不知道它是否是 #hashtag@mention 的结尾。还有 #hashtag"" 如果我用 @mention

开始我的文本,则永远不会调用语句

范围和一切都工作得很好,但是当我键入空格时,它不会重置以启动新的 #hashtag@mention

我需要重置范围还是我必须做什么?

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

    if textView == postCommentTextView {
        if textView.text == "#" {
            recordingHash = true
            recordingMention = false
            startParseHash = range.location

        } else if textView.text == "@" {
            recordingHash = false
            recordingMention = true
            startParseMention = range.location

        } else if textView.text == " " {
            recordingHash = false
            recordingMention = false
            createdHashesArray.append(createdHashString)
            createdHashString = ""
        }


        if recordingHash != nil {
            if recordingHash == true {
                var value = String()
                print("COUNT: \(textView.text.count)")
                if startParseHash <= (textView.text.count - startParseHash) {
                    let createdRange = NSMakeRange(startParseHash, textView.text.count - startParseHash)
                    value = textView.text(in: createdRange.toTextRange(textView)!)! //as! NSString
                    createdHashString = "\(value)"
                   // print("hash:\(createdHashString)")
                }
                print("hash:\(createdHashString)")
                if textView.text.count == 0 {
                    recordingHash = false
                    recordingMention = false
                   // createdHashesArray.append(createdHashString)
                    createdHashString = ""
                }
            }
        }

    return true
}

最佳答案

当您检查字符时,您应该使用文本变量而不是 textView.texttext 为您提供当前字符(或粘贴的整个文本) textview.text 为您提供整个文本

关于ios - 调用 UITextViewDelegate ShouldTextChangeIn 时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47781417/

相关文章:

ios - 当 firebase 没有任何东西可供抓取时,如何停止网络指示器以停止旋转

iphone - UITableView 低于代码全屏?

ios - 单击按钮后跳转到该详细信息时,如何为 UITableViewCell 创建详细信息?

ios - 如何理解并修复这个错误?

ios - Segue 可选值 nil

ios - CTFontManagerUnregisterGraphicsFont 返回 true 但内存未释放

ios - Collection View 中的自定义 UITextField 未显示

ios - 如何修改另一个类的数组

ios - 单元格背景图片已下载,但下载完成后需要 5 秒才能显示

ios - 如何防止iOS的NSDictionary中的订单更改