我有一个 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.text
。 text
为您提供当前字符(或粘贴的整个文本) textview.text
为您提供整个文本
关于ios - 调用 UITextViewDelegate ShouldTextChangeIn 时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47781417/