TextField得到输入框的内容(代理内) 解决密码输入退格变清空

NSString * toBeString = [textField.textstringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容

if (range.location > 0 && range.length == 1 && string.length == 0)
    {
        // Stores cursor position
        UITextPosition *beginning = textField.beginningOfDocument;
        UITextPosition *start = [textField positionFromPosition:beginningoffset:range.location];
        NSInteger cursorOffset = [textField offsetFromPosition:beginningtoPosition:start] + string.length;
        
        // Save the current text, in case iOS deletes the whole text
        NSString *text = textField.text;
        
        // Trigger deletion
        [textField deleteBackward];
        
        
        // iOS deleted the entire string
        if (textField.text.length != text.length - 1)
        {
            textField.text = [text stringByReplacingCharactersInRange:rangewithString:string];
            
            // Update cursor position
            UITextPosition *newCursorPosition = [textFieldpositionFromPosition:textField.beginningOfDocument offset:cursorOffset];
            UITextRange *newSelectedRange = [textFieldtextRangeFromPosition:newCursorPosition toPosition:newCursorPosition];
            [textField setSelectedTextRange:newSelectedRange];
        }
        return NO;
    }

你可能感兴趣的:(TextField得到输入框的内容(代理内) 解决密码输入退格变清空)