首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用UITextViewDelegate ShouldTextChangeIn时的奇怪行为

调用UITextViewDelegate ShouldTextChangeIn时的奇怪行为
EN

Stack Overflow用户
提问于 2017-12-12 20:58:28
回答 1查看 40关注 0票数 0

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

问题是,一旦调用了委托方法,我就不能重置它来签入其他if语句。例如," "空间字符“如果我通过#hashtag__启动文本,就不会调用if语句”,因此我无法知道它是#hashtag的结尾还是@mention的结尾。此外,如果我用#hashtag启动文本,则不会调用" "@mention语句。

范围和一切都很好,但是当我输入空间时,就不会重新启动一个新的#hashtag@mention

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

代码语言:javascript
复制
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
}
EN

回答 1

Stack Overflow用户

发布于 2017-12-12 21:12:43

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47781417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档