iOS Dev
iOS Dev

Reputation: 4248

Check if input text is empty in custom keyboard for iOS 8

I'm developing a custom keyboard for iOS 8, and I would like to check if input text is empty or not, in order to enable or disable return key button, if it's needed (self.textDocumentProxy.enablesReturnKeyAutomatically == YES).

This is what I've done so far:

- (void)textDidChange:(id<UITextInput>)textInput
{
    NSString *inputText = [self.textDocumentProxy.documentContextBeforeInput stringByAppendingString:self.textDocumentProxy.documentContextAfterInput];

    if (self.textDocumentProxy.enablesReturnKeyAutomatically)
    {
        self.returnButton.enabled = !(inputText.length == 0);
    }
    else
    {
        self.returnButton.enabled = YES;
    }
}

But if "Auto-Enable Return Key" is ON, return button is always disabled, even if input text is not empty. What is the right way to check if input text is empty or not? Thanks.

Upvotes: 1

Views: 1025

Answers (1)

Anand Suthar
Anand Suthar

Reputation: 3798

Try this, again tested

Disable your return key in below function which perform your deleteBackward operation

- (IBAction)returnBackSpacePressed
{
    [self.textDocumentProxy deleteBackward];

    if(self.textDocumentProxy.documentContextBeforeInput.length-1 == 0)
     {
       [self.textDocumentProxy insertText:@"Now disable your return key"];
       // Here your inputTest is now empty
     }
}

End enable your return key when ever your insertTest

- (void)putChar:(NSString *)charactor
{
   [self.textDocumentProxy insertText:charactor];
// enable your return key here again, because now your inputText is not empty
}

Upvotes: 2

Related Questions