0

I'm currently using a UITextfield to manage the software keyboard and the predictive text, etc.

In other apps, whenever you press space twice, it gets replaced by ". "

But whenever I do that with the UITextfield, I get both callback in shouldChangeCharactersInRange, wanting to add a space ...twice.

The second space is ignored by the UITextField.

I don't get any callback asking me to replace it by " ." Is this normal? How are we suppose to handle that case?

I can always do the string replacement myself, but how do we know if it's enabled or not?

Is there a way to retrieve that setting value in the system settings?

Thanks.

UPDATE : Adding some code I currently have.

bool CYIKeyboardInputBridge_iOS::ShowKeyboardInternal(Receiver *pSource) {

UITextField *postField = m_pData->m_postField;

if (postField == nil)
{
    m_pData->m_pKeyboardDelegate = [[KeyboardDelegate_iOS alloc] init];
    m_pData->m_pKeyboardDelegate->m_pBridge = this;
    postField = [[UITextField alloc] init];
    postField.delegate = m_pData->m_pKeyboardDelegate;
    m_pData->m_postField = postField;
}

[[YiRootViewController sharedInstance].view addSubview:postField];

CYIKeyboardInputBridge::Receiver *pKeyboardReceiver = GetCurrentReceiver();
CYIKeyboardInputBridge::RETURN_KEY_TYPE keyType = pKeyboardReceiver->GetReturnKeyType();

switch (keyType)
{
    case CYIKeyboardInputBridge::RETURN_KEY_DEFAULT:
        postField.returnKeyType = UIReturnKeyDefault;
        break;
    case CYIKeyboardInputBridge::RETURN_KEY_GO:
        postField.returnKeyType = UIReturnKeyGo;
        break;
    case CYIKeyboardInputBridge::RETURN_KEY_SEARCH:
        postField.returnKeyType = UIReturnKeySearch;
        break;
    case CYIKeyboardInputBridge::RETURN_KEY_NEXT:
        postField.returnKeyType = UIReturnKeyNext;
        break;
    case CYIKeyboardInputBridge::RETURN_KEY_DONE:
        postField.returnKeyType = UIReturnKeyDone;
        break;
}

CYIKeyboardInputBridge::INPUT_TYPE inputType = pKeyboardReceiver->GetInputType();
switch (inputType) {
    case CYIKeyboardInputBridge::INPUT_EMAIL:
        postField.keyboardType = UIKeyboardTypeEmailAddress;
        break;

    case CYIKeyboardInputBridge::INPUT_NUMBER:
        postField.keyboardType = UIKeyboardTypeNumberPad;
        break;

    case CYIKeyboardInputBridge::INPUT_PHONE:
        postField.keyboardType = UIKeyboardTypePhonePad;
        break;

    case CYIKeyboardInputBridge::INPUT_URI:
        postField.keyboardType = UIKeyboardTypeURL;
        break;

    case CYIKeyboardInputBridge::INPUT_TEXT:
    case CYIKeyboardInputBridge::INPUT_PASSWORD:
    default:
        postField.keyboardType = UIKeyboardTypeDefault;
        break;
}

// This will disable predictions on the virtual keyboard.
if (inputType == CYIKeyboardInputBridge::INPUT_PASSWORD)
{
    [postField setSecureTextEntry:true];
    postField.autocorrectionType = UITextAutocorrectionTypeNo;
    postField.spellCheckingType = UITextSpellCheckingTypeNo;
}
else
{
    [postField setSecureTextEntry:false];
    postField.autocorrectionType = UITextAutocorrectionTypeDefault;
    postField.spellCheckingType = UITextSpellCheckingTypeDefault;
}

[postField reloadInputViews];

CYIKeyboardInputBridge::Receiver::Description receiverDescription;
pKeyboardReceiver->OnSynchronizeKeyboard(receiverDescription);
[postField setText:receiverDescription.defaultText.ToNSString()];
m_uMaximumCharacters = receiverDescription.nMaximumCharacterCount;

SynchronizeCursor(receiverDescription.nCurrentCursorPosition);

[[YiRootViewController sharedInstance].view resignFirstResponder]; 
[postField becomeFirstResponder];

return true;
}

These are my callback functions:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    YI_UNUSED(textField);
    m_pBridge->OnKeyboardDidShow();
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    YI_UNUSED(textField);
    m_pBridge->OnKeyboardDidHide();
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    CYIKeyboardInputBridge::Receiver *pReceiver = m_pBridge->GetCurrentReceiver();
    if (pReceiver)
    {
        pReceiver->OnTextReplaced(CYIString([textField text]), 0);
    }            

    return true;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    bool bShouldUpdate = true;
    CYIString textToAdd = CYIString(string);
    const NSUInteger uNewLength = [textField.text length] + [string length] - range.length;

    if ((range.location > 0 && [string length] > 0 &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[string characterAtIndex:0]] &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textField text] characterAtIndex:range.location - 1]]))
    {
        //Manually replace the space with your own space, programmatically
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@" "];

        bShouldUpdate = false;
    }

    if (uNewLength <= m_pBridge->GetMaximumCharacters())
    {
        CYIKeyboardInputBridge::Receiver *pReceiver = m_pBridge->GetCurrentReceiver();

        if (pReceiver)
        {
            if (textToAdd.GetLength() > 1)
            {
                CYIString text = CYIString([textField text]);

                CYIString beforeInsert = text.SubStr(0, (YI_INT32)range.location);

                CYIString afterInsert = text.SubStr((YI_INT32)(range.location + range.length));

                CYIString newText = beforeInsert + textToAdd + afterInsert;

                textField.text = newText.ToNSString();

                bShouldUpdate = false;
            }

            m_pBridge->SetBlockCursorUpdate(true);

            if (range.length > 0)
            {
                pReceiver->OnTextDeleted(YI_INT32(range.length), 0);
            }

            if (textToAdd.GetLength() > 0)
            {
                pReceiver->OnTextEntered(textToAdd, 1);
            }

            m_pBridge->SetBlockCursorUpdate(false);

            if (!bShouldUpdate)
            {
                CYIKeyboardInputBridge::Receiver::Description receiverDescription;
                pReceiver->OnSynchronizeKeyboard(receiverDescription);

                m_pBridge->SynchronizeCursor(receiverDescription.nCurrentCursorPosition);
            }
        }
    }

    return bShouldUpdate;
}
KniForz
  • 83
  • 8
  • please share your code. – f_qi Mar 13 '17 at 17:24
  • Looks like your answer should be here: http://stackoverflow.com/questions/2576561/iphone-disable-the-double-tap-spacebar-for-shortcut – DonMag Mar 13 '17 at 17:28
  • I added some code. DonMag, that answer doesn't seem to work for shouldChangeCharactersInRange. – KniForz Mar 13 '17 at 19:00
  • hmmm.... Seems to work fine in my test. Plain UITextField, double-tap space, replace with space if appropriate. Have you tried it in a new, plain test? – DonMag Mar 13 '17 at 20:06

0 Answers0