[英]iOS 8 textDocumentProxy - able to delete more than one space?
I would like to delete multiple spaces from self.textDocumentProxy
when working with my extension's KeyboardViewController
, and was wondering if there is a Apple-supported method that specifically performs this action? 在使用扩展程序的
KeyboardViewController
,我想从self.textDocumentProxy
删除多个空格,并且想知道是否有Apple支持的方法专门执行此操作?
So far, I have been using a pretty "hacky" way of doing the following (here it deletes all the previous characters found on textDocumentProxy
): 到目前为止,我一直在使用一种非常“ hacky”的方式来执行以下操作(在此,它会删除在
textDocumentProxy
上找到的所有先前字符):
for (int i = 0; i < self.textDocumentProxy.documentContextBeforeInput.length; i++){
[self.textDocumentProxy deleteBackward];
}
The issue with this lies in the method deleteBackward
, which, depending on what prompt is given, always deletes around half (its quite reliable, especially when documentContextBeforeInput
is longer than 20 characters) of the total number of times its told to delete. 这样做的问题在于
deleteBackward
方法,该方法根据给出的提示,总是删除被告知要删除的总次数的一半左右(特别可靠,尤其是documentContextBeforeInput
大于20个字符时)。 Since this is rather unreliable, I was wondering if there is a way to easily delete multiple spaces, or all of the texted in textDocumentProxy.documentContextBeforeInput
由于这是相当不可靠的,我想知道是否有一种方法可以轻松删除多个空格,或
textDocumentProxy.documentContextBeforeInput
所有textDocumentProxy.documentContextBeforeInput
Thanks! 谢谢!
There is a fundamental issue in the loop you're using: 您正在使用的循环中存在一个基本问题:
for (int i = 0; i < self.textDocumentProxy.documentContextBeforeInput.length; i++){
[self.textDocumentProxy deleteBackward];
}
The i < self.textDocumentProxy.documentContextBeforeInput.length
check is performed multiple times. i < self.textDocumentProxy.documentContextBeforeInput.length
检查多次执行。 And the .length
attribute is actually decreasing by 1 for every deleteBackward
you do. 而
.length
属性实际上对于您执行的每个deleteBackward
减少1。 i
however is merrily increasing by 1 for each iteration. 但是,
i
每次迭代都会增加1。
As a result only half will be deleted. 结果,只有一半将被删除。
You can flip the order around to fix the issue. 您可以翻转订单以解决此问题。
for (int i = self.textDocumentProxy.documentContextBeforeInput.length; i > 0; i--){
[self.textDocumentProxy deleteBackward];
}
You can also cache the original length of the textDocument before you start changing it. 您也可以在开始更改文本文档之前缓存其长度。
Maybe try this solution that will erase everything in the input text, not only the text before the cursor ;) 也许尝试这种解决方案,它将删除输入文本中的所有内容,而不仅仅是光标之前的文本;)
func deleteInputText() {
if let afterInput = self.textDocumentProxy.documentContextAfterInput {
self.textDocumentProxy.adjustTextPositionByCharacterOffset(afterInput.characters.count)
}
while let _=self.textDocumentProxy.documentContextBeforeInput {
self.textDocumentProxy.deleteBackward()
}
}
while (self.textDocumentProxy.hasText==YES)
{
[self.textDocumentProxy deleteBackward];
}
should remove all the text. 应该删除所有文本。
[self.textDocumentProxy deleteBackward];
deletes only 1 character. 仅删除1个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.