1

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?

So far, I have been using a pretty "hacky" way of doing the following (here it deletes all the previous characters found on 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. 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

Thanks!

daspianist
  • 5,336
  • 8
  • 50
  • 94

3 Answers3

1

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. And the .length attribute is actually decreasing by 1 for every deleteBackward you do. i however is merrily increasing by 1 for each iteration.

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.

EWit
  • 1,954
  • 13
  • 22
  • 19
  • Doesn't work with long text. I tried many times with long text but sometimes it didn't clear all the text. Random some text are left – TomSawyer Nov 22 '14 at 10:05
  • I tried many times, but with long text, random text still remain. but if i check hasText() will return false. That's really weird, it's seems error of ios – TomSawyer Nov 22 '14 at 18:49
  • Well that simply means that there is something else going on. And my experience with iOS has never extended to handling keyboard events or even doing a lot of input handling. So I won't be of much use there. – EWit Nov 22 '14 at 19:34
  • i entered only two lines of text (80 characters) but i couldn't complete clear all of it. Random few characters left remain. – TomSawyer Nov 22 '14 at 19:36
0

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()


    }

}
RomOne
  • 2,065
  • 17
  • 29
-1
while (self.textDocumentProxy.hasText==YES)
{    
  [self.textDocumentProxy deleteBackward];      
}

should remove all the text.

[self.textDocumentProxy deleteBackward]; deletes only 1 character.

user2314737
  • 27,088
  • 20
  • 102
  • 114
Rabiea
  • 31
  • 1