swift - 在不破坏撤消堆栈的情况下在 NSTextView 中附加文本
问题描述
我有一个基于 NSTextView 的应用程序,其中有各种更改文本的命令。为了替换一部分,我使用以下代码段:
if ([textView shouldChangeTextInRange:range replacementString:string]) { //string
[textStorage beginEditing];
[textStorage replaceCharactersInRange:range withAttributedString:attributedString];
[textStorage endEditing];
[textView didChangeText];
}
当我想在文本视图的末尾追加时,我的问题就开始了。在那种情况下,我不能使用shouldChangeTextInRange:range replacementString:string
,所以我使用:
textView.textStorage?.beginEditing()
self.textView.textStorage?.append(NSAttributedString(string: string))
self.textView.scrollToEndOfDocument(nil)
textView.textStorage?.endEditing()
textView.didChangeText()
虽然它适用于属性,但 Undo 完全中断。知道如何解决这个问题吗?
解决方案
推荐阅读
- python - 在 Mac OS(M1 和英特尔 CPU)上使用 pipenv 安装 mysqlclient 永远不会成功
- c - 未初始化的读取是什么意思?
- python - TensorFlow 给出“
不是此图的元素”从另一个类调用时出错 - sql - 一旦在查询中输入,如何使记录只读?
- php - remove_action wc_empty_cart_message 在 WooCommerce 中不起作用
- d3.js - D3如何修复树形图中没有根?
- woocommerce - 报告中的“客户”栏是否可以轻松更改为“公司名称”?
- jekyll - Liquid - 有条件地包含来自 _includes 文件夹的文件
- google-cloud-platform - BigQuery - JSONpath 递归运算符 (1/2)
- spring - Springfox 3.0 在 Spring 框架中不起作用