输入两次击中后删除属性字符串

问题描述

我正在编写笔记应用程序,但遇到问题。当用户在写完文本后按下Enter键时,光标会向下一行并输入一个新的复选框(工作正常)。当用户在新的复选框后单击Enter而不输入任何内容时,它应删除该复选框并将光标返回到行的开头,以便用户可以正常键入。这是我到目前为止的代码,但是没有用

func textView(_ textView: UITextView,shouldChangeTextIn range: NSRange,replacementText text: String) -> Bool {
        if let lastLine = NSMutableAttributedString(attributedString: attributedText).mutableString.components(separatedBy: "\n").last /*string.components(separatedBy: "\n").last  */{
            if lastLine == checkedBoxString.string && text == "\n" && checkBoxMode {
                checkBoxMode = false
                let mutableText = NSMutableAttributedString(attributedString: attributedText)
                let range = selectedRange
                print(range)
                mutableText.deleteCharacters(in: NSRange(location: selectedRange.location - 3,length: selectedRange.length))
                attributedText = mutableText
                return false
            } else if text == "\n" && checkBoxMode {
                let mutableText = NSMutableAttributedString(attributedString: attributedText)
                mutableText.append(NSAttributedString(string: "\n"))
                attributedText = mutableText
                print(selectedRange)
                makeCheckBox()
                return false
            }
        }
        return true
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)