从 shouldChangeTextIn returns 字符更新 UITextView 值两次

Updating UITextView value from shouldChangeTextIn returns character twice

我想在 UITextView 中实现 HasTag,而用户在 UITextView 中键入文本。

为此,我尝试了以下代码。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text == "\n" {
        self.txtB.resignFirstResponder()
        return false
    }

    let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
    let numberOfChars = newText.count

    self.txtB.attributedText = CommonHelper.sharedInstance.convert(self.txtB.text.findMentionText(), string: self.txtB.text)
    return numberOfChars < 121 //120 limit
}

HasTag 函数

func findMentionText() -> [String] {
    var arr_hasStrings:[String] = []
    let regex = try? NSRegularExpression(pattern: "(#[a-zA-Z0-9_\p{Arabic}\p{N}]*)", options: [])
    if let matches = regex?.matches(in: self, options:[], range:NSMakeRange(0, self.count)) {
        for match in matches {
            arr_hasStrings.append(NSString(string: self).substring(with: NSRange(location:match.range.location, length: match.range.length )))
        }
    }
    return arr_hasStrings
}


func convert(_ hashElements:[String], string: String) -> NSAttributedString {
    let hasAttribute = [NSAttributedString.Key.foregroundColor: UIColor.orange, NSAttributedString.Key.font: UIFont.init(name: Fonts.PoppinsBoldItalic, size: 16.0)]
    let normalAttribute = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.init(name: Fonts.PoppinsBoldItalic, size: 16.0)]

    let mainAttributedString = NSMutableAttributedString(string: string, attributes: normalAttribute)
    let txtViewReviewText = string as NSString

    hashElements.forEach { if string.contains([=12=]) {
        mainAttributedString.addAttributes(hasAttribute, range: txtViewReviewText.range(of: [=12=]))
        }
    }
    return mainAttributedString
}

如果我键入单个字符,那么 textview 会将该字符显示两次。

如何解决这个问题?

在下面几行中,

self.txtBeep.attributedText = CommonHelper.sharedInstance.convert(self.txtB.text.findMentionText(), string: self.txtB.text)

return numberOfChars < 121 //120 limit
  1. line-1 中,您根据需要在 textView 中设置 text

  2. line-2中,如果numberOfChars < 121true将是returned,即append the text to the textView.

这就是 texttextView 中输入两次以防 numberOfChars < 121 的原因。

解法:

textView(_:shouldChangeTextIn:replacementText:)方法必须像,

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text == "\n" {
        self.txtB.resignFirstResponder()
        return false
    }

    let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
    let numberOfChars = newText.count
    if numberOfChars > 120 {
        return false
    }

    self.txtB.attributedText = CommonHelper.sharedInstance.convert(newText.findMentionText(), string: newText)
    return false
}