1

I'm trying to delete an entire word in a text field when I do a long press on the delete key.

I attempted to run a while loop to check for a space (" "), and then delete any char that did not match space (" ") - but...

1) I am not sure if how I'm attempting to search the text field is correct

2) The way I'm attempting my loop is broken because of that

func delLong(){
    var proxy = textDocumentProxy as UITextDocumentProxy
    while [-1] != " "{
        proxy.deleteBackward()
    }
}
glasstongue
  • 103
  • 1
  • 1
  • 8
  • Are you looking for a find and replace method? Something that finds the string and then replaces it –  Jan 22 '15 at 06:23
  • Thomas - It freezes my interface currently. ... TheCamp - No, I'm looking to delete all the characters to the left until it reaches a space " " character. – glasstongue Jan 22 '15 at 06:26
  • So where does `textDocumentProxy` come from? – qwerty_so Jan 22 '15 at 06:28
  • I think it's a custom keyboard proxy inside of swift - apples info on the command ... A text document proxy provides textual context to a custom keyboard (which is based on the UIInputViewController class) by way of the keyboard’s textDocumentProxy property. – glasstongue Jan 22 '15 at 06:33
  • I'm just an OSX developer (no IOS) but have you put a print statement inside the loop? – qwerty_so Jan 22 '15 at 07:44
  • I think my loop is fundamentally broken because I don't know how to get the value I'm looking for haha - lots more reading I guess ( http://stackoverflow.com/questions/24029163/finding-index-of-character-in-swift-string is the closest I've found so far) – glasstongue Jan 22 '15 at 08:09

1 Answers1

3

I haven't tested, but you can try:

func delLong(){
    var proxy = textDocumentProxy as UITextDocumentProxy
    while proxy.hasText() {
        let beforeText = proxy.documentContextBeforeInput
        if beforeText.isEmpty || beforeText.hasSuffix(" ") {
            break
        }
        proxy.deleteBackward()
    }
}
rintaro
  • 51,423
  • 14
  • 131
  • 139