【问题标题】:Strange behavior while calling UITextViewDelegate ShouldTextChangeIn调用 UITextViewDelegate ShouldTextChangeIn 时出现奇怪的行为
【发布时间】:2018-05-26 15:47:15
【问题描述】:

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

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

测距和一切都工作得很好,但是当我输入空格时它不会重置以开始新的#hashtag@mention

我需要重置range 还是我需要做什么?

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
}

【问题讨论】:

    标签: ios swift uitableview uitextview hashtag


    【解决方案1】:

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

    【讨论】:

    • 是的...我必须检查角色的value 然后从那里开始。
    • 代码确实需要完全正确地重做。请记住,用户可以粘贴文本,因此不要只假设一次输入一个字符。当然,用户也可以删除。最后,不要忘记用户可以选择任何文本,然后键入或粘贴。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2020-01-19
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多