我有一个UITableViewCell,在这里我有一个评论UITextView来写评论。在写下注释时,我查看了#hashtags和@mentions,以检查它们在应用程序服务器端的可用性,并将它们显示在tableView中。
问题是,一旦调用了委托方法,我就不能重置它来签入其他if语句。例如," "空间字符“如果我通过#hashtag__启动文本,就不会调用if语句”,因此我无法知道它是#hashtag的结尾还是@mention的结尾。此外,如果我用#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
}发布于 2017-12-12 21:12:43
当您检查字符时,应该使用文本变量而不是textView.text。text为您提供当前字符(或粘贴到的整个文本),textview.text为您提供整个文本
https://stackoverflow.com/questions/47781417
复制相似问题