从 shouldChangeTextIn 更新 UITextView 值返回字符两次

Kul*_*eep 0 uitextview hashtag swift

我想在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
}
Run Code Online (Sandbox Code Playgroud)

哈希标签函数

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($0) {
        mainAttributedString.addAttributes(hasAttribute, range: txtViewReviewText.range(of: $0))
        }
    }
    return mainAttributedString
}
Run Code Online (Sandbox Code Playgroud)

如果我输入单个字符,则文本视图会显示该字符两次。

如何解决这个问题?

PGD*_*Dev 5

在下面几行中,

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

return numberOfChars < 121 //120 limit
Run Code Online (Sandbox Code Playgroud)
  1. line-1中,您根据您的要求进行text设置。textView

  2. 第 2 行中,如果numberOfChars < 121, truewill be returned,这意味着append the text to the textView

这就是在in casetext中输入两次的原因。textViewnumberOfChars < 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
}
Run Code Online (Sandbox Code Playgroud)