美文网首页
TextField得到输入框的内容(代理内) 解决密码输入退格变

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

作者: InitWithYou | 来源:发表于2016-09-22 11:05 被阅读25次
    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得到输入框的内容(代理内) 解决密码输入退格变

          本文链接:https://www.haomeiwen.com/subject/rrrzettx.html