UITextField"。"快捷方式回调不起作用

时间:2017-03-13 17:02:10

标签: ios objective-c uitextfield uitextfielddelegate

我目前正在使用UITextfield来管理软件键盘和预测文本等。

在其他应用中,无论何时按两次空格,都会被"取代。 "

但每当我使用UITextfield执行此操作时,我会在shouldChangeCharactersInRange中获得回调,并希望添加空格......两次。

UITextField会忽略第二个空格。

我没有得到任何回电要求我用" " 这是正常的吗? 我们如何处理这种情况呢?

我可以随时自行更换字符串,但我们如何知道它是否已启用?

有没有办法在系统设置中检索该设置值?

感谢。

更新:添加我目前拥有的一些代码。

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;
}

这些是我的回调函数:

- (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;
}

0 个答案:

没有答案