- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个自定义的 NSTextView 子类,还有一个自定义的 NSTextStorage 组件。 NSTextStorage 根据上下文修改用户输入的文本。
因为最终文本可能比用户最初输入的文本短,所以我不得不覆盖 insertText:replacementRange
在我的 NSTextView 中。一个最小的例子是:
- (void) insertText:(id)string replacementRange:(NSRange)replacementRange {
if ([self hasMarkedText]) {
[[self textStorage] replaceCharactersInRange:[self markedRange] withString:string];
} else {
[[self textStorage] replaceCharactersInRange:[self selectedRange] withString:string];
}
[self didChangeText];
}
这在几个月的广泛测试中工作正常......除了自动拼写检查和更正被禁用。 “波浪形”不会出现在拼写错误的单词下方,除非我停止键入、移动鼠标并将焦点切换到我的应用程序或从我的应用程序切换焦点。几秒钟后,整个 TextView 都进行了拼写检查。因为它是事后发生的,所以自动更正当然是禁用的。
如果我禁用自定义 insertText:replacementRange:
方法,其他一切正常,自动拼写功能返回。我只需要注意不要触发导致缩短文本的更改,因为它会触发属性超出范围错误(首先是我的自定义方法的原始原因。)
显然是 Apple 对 insertText:replacementRange:
的实现比我做的更多。我在 [self checkTextInRange...]
上尝试了多种变体, [self checkTextInSelection:]
等。它们都没有恢复正常的功能。
搜索 Apple 的文档并不能帮助我找到我从我的方法中遗漏的导致拼写检查中断的内容。任何指示或想法将不胜感激!!
提前致谢!
编辑:以下是我的 NSTextStorage 提供的各种行为的一些示例。 (|
代表插入符号)
开始于:
* item
* |
如果我按下回车键,我会得到以下结果(删除 *<space>
):
* item
|
另一个例子,如果启用“更改跟踪”:
this is thee| time
如果我点击删除:
this is the|{--e--} time
如您所见,一次击键可能会导致在文本中添加或删除多个字符。
编辑 2:仅供引用——当在文档末尾按回车键时发生缩短时,我遇到的属性超出范围的问题——NSTextview 尝试设置新的段落样式才发现文档比预期的要短。我找不到更改 NSTextview 目标范围的方法。
最佳答案
我有一个部分解决方案。
在我的自定义 insertText:replacementRange:
方法中,在 didChangeText
之前:
NSinteger wordCount;
NSOrthography * orthography;
static NSInteger theWordCount;
NSOrthography * orthography;
NSRange spellingRange = <range to check>
NSArray * results = [[NSSpellChecker sharedSpellChecker] checkString:[[self textStorage] string]
range:spellingRange
types:[self enabledTextCheckingTypes]
options:NULL
inSpellDocumentWithTag:0
orthography:&orthography
wordCount:&theWordCount];
if (results.count) {
[self handleTextCheckingResults:results forRange:spellingRange types:[self enabledTextCheckingTypes] options:@{} orthography:orthography wordCount:theWordCount];
}
然而,这是不完整的:
关于objective-c - 自定义 NSTextView insertText :replacementRange breaks Spell Checking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45705424/
我正在制作自定义键盘。我想要一个自动周期功能,但我的代码有错误,请帮我解决 代码: @IBAction func autoPeriod(button: UIButton) { (textDoc
function insertText(elemID, text) { var elem = document.getElementById(elemID); elem.innerHT
我在 UITextView 上有一个自定义的 InputView,它打开了自动更正和自动大写。 我的自定义输入 View 有一堆 UIButtons,它们在 UITextView 上调用 Insert
我正在使用应用程序脚本侧边栏插入文本,我在其中输入需要在开头附加一些文本,然后再次输入后附加。 附加文本将由侧边栏中的文本框确定。 我将值作为 formObject 传递 function sendf
到目前为止我发现的唯一方法是document.queryCommandSupported('insertText'),尽管它看起来不正确,例如此代码在firefox中不起作用,但上面的命令返回true
我有一个输入,以及我的 JavaScript 函数: function insertText(elemID, text) { var ele
我有一个 NSTextField,我想在其中的插入点输入文本。我有一个不会产生错误的代码: let str = "Abc" myTextField.insertText(str) [编
我知道 FireFox 可以很好地处理 execCommand 但这次我想我遇到了一个错误。 但也许我只是忽略了一些东西。 我确实有一个 textarea,我正在其中执行 insertText 只要我
我有表情符号列表。他们每个人都有自己的unicode。使用 Modifier.insertText(),我想将它们插入到文本中。 _addEmoji(text) { const { edito
我在 summernote 工具栏中启用了 codeview。 以及自定义菜单的代码(用于将自定义参数插入编辑器): let event = ui.buttonGroup([ ui.butto
我尝试在使用 setData("") 清除内容后插入一些内容,然后我使用 insertText 方法向其中插入一些内容。但内容仍然是空的。 相关代码: CKEDITOR.instances.conte
我想观察 UITextfield 中的文本输入子类。为此,我正在尝试实现 UIKeyInput该子类中的协议(protocol)方法。但是 insertText:和 hasText方法永远不会被调用。
我想复制一份 Google 文档并在副本中添加文本。这是我的代码: function main() { var template = DriveApp.getFileById(TEMPLATE
我有一个包含 QTextDocument 的 QTextEdit,它正在使用 QTextCursor 界面以编程方式进行编辑。正在使用 QTextCursor::insertText() 编辑文档。
我计划在我的网站中实现 Quill,但不幸的是 insertText 函数会产生以下结果: TypeError: n.appendChild is not a function shadow.ts:1
我正在尝试在 Swift 中构建一个自定义键盘,它只做一件事,而且只做一件事。按下键盘上的按钮时插入 ¯_(ツ)_/¯。 这是我拥有的功能: func buttonPressed() { // A
我已经创建了一个 iMessage 应用程序来发送带有链接的图像。而且我不希望接收方强制拥有相同的应用程序。 在 IMessage 扩展中,有什么方法可以调用 MSMessagesAppViewCon
我在我的项目中使用 Angular 8,并尝试实现ngx-CKEditor。我需要在编辑器中添加自定义文本,这应该发生当我单击列表时,我需要在 CKEditor 中添加内容。 我尝试使用此代码,但不适
当用户编辑 contenteditable div 并按下某些键时,我想覆盖默认行为。例如,我想在用户按下 ENTER 时插入一个普通的换行符。我使用 document.execCommand("in
我有一个自定义的 NSTextView 子类,还有一个自定义的 NSTextStorage 组件。 NSTextStorage 根据上下文修改用户输入的文本。 因为最终文本可能比用户最初输入的文本短,
我是一名优秀的程序员,十分优秀!