Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextInputDelegate methods not functioning correctly

I'm working on iOS 8 custom keyboard extension right now, and there are some issues in using UITextInputDelegate Methods.

Does this right: selectionWillChange: and selectionDidChange: methods should be called when user long-presses typing area? And textWillChange: and textDidChange: methods should be called whenever the text is literally changing?

Actually, what I observed is that, when I changed selection in text input area, textWillChange: and textDidChange: are called, and I cannot get a clue that the other two methods are called in what condition. If anyone knows about the usage of these delegate methods, please let me know.

like image 333
Tack-Gyu Lee Avatar asked Aug 06 '14 10:08

Tack-Gyu Lee


1 Answers

It's not working for me either... what I am currently doing is just using textWillChange and textDidChange which does get called, as you mentioned, when you change your selection... (they get called BEFORE and AFTER)

And then comparing the:
self.textDocumentProxy.documentContextBeforeInput
self.textDocumentProxy.documentContextAfterInput

From BEFORE (textWillChange) to the AFTER (textDidChange) to see if selection range or length changed at all.


Something like this (set the 4 NSStrings below in your .h file of course... haven't tested this exact snippet because I wrote it from scratch just now on SO.com but I'm sure the principle works if I made any errors)

- (void)textWillChange:(id<UITextInput>)textInput {
    beforeStringOfTextBehindCursor = self.textDocumentProxy.documentContextBeforeInput;
    beforeStringOfTextAfterCursor = self.textDocumentProxy.documentContextAfterInput;
}

- (void)textDidChange:(id<UITextInput>)textInput {
    afterStringOfTextBehindCursor = self.textDocumentProxy.documentContextBeforeInput;
    afterStringOfTextAfterCursor = self.textDocumentProxy.documentContextAfterInput;

    BOOL didSelectionChange = NO;

    if (![beforeStringOfTextBehindCursor isEqualToString:afterStringOfTextBehindCursor]) {
        didSelectionChange = YES;
    }

    if (![beforeStringOfTextAfterCursor isEqualToString:afterStringOfTextAfterCursor]) {
        didSelectionChange = YES;
    }

    if (didSelectionChange) {
        NSLog(@"Selection Changed!");
    }   
}
like image 100
Albert Renshaw Avatar answered Nov 19 '22 22:11

Albert Renshaw