【问题标题】:How can I force a UITextField to only support lowercase characters?如何强制 UITextField 仅支持小写字符?
【发布时间】:2014-09-18 01:21:46
【问题描述】:

我想覆盖按键事件以强制即使按下大写键,结果输出仍然是小写。

【问题讨论】:

    标签: objective-c uitextfield


    【解决方案1】:

    这应该可以(您还应该设置 textfield.autocapitalizationType=UITextAutocapitalizationTypeNone)

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        if (textField==_usernameTextfield || textField==_emailTextfield) {
            NSRange lowercaseCharRange;
            lowercaseCharRange = [string rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];
    
            if (lowercaseCharRange.location != NSNotFound) {
                // some uppercase chars where found, need to replace
    
                UITextPosition *beginning = textField.beginningOfDocument;
                UITextPosition *start = [textField positionFromPosition:beginning offset:range.location];
                // new cursor location after insert/paste/typing
                NSInteger cursorOffset = [textField offsetFromPosition:beginning toPosition:start] + string.length;
    
                // convert whole text to lowercase and update the textfield
                textField.text = [textField.text stringByReplacingCharactersInRange:range
                                                                     withString:[string lowercaseString]];
    
                // now reposition the cursor
                UITextPosition *newCursorPosition = [textField positionFromPosition:textField.beginningOfDocument offset:cursorOffset];
                UITextRange *newSelectedRange = [textField textRangeFromPosition:newCursorPosition toPosition:newCursorPosition];
                [textField setSelectedTextRange:newSelectedRange];
                return NO;
            }
        }
        return YES;
    }
    

    【讨论】:

    • 我赞成你的回答。但你应该检查这个接受的答案stackoverflow.com/questions/21092182/…。你只需要 textField.text = [textField.text stringByReplacingCharactersInRange:range withString:[string lowercaseString]];并返回 NO;
    【解决方案2】:

    解决方案是为textFieldDidChange添加一个选择器:

    [self addTarget:self
                      action:@selector(textFieldDidChange:)
            forControlEvents:UIControlEventEditingChanged];
    

    textFieldDidChange 事件中,添加以下内容:

    - (void)textFieldDidChange:(UITextField*)textField {
        textField.text = [textField.text lowercaseString];
    }
    

    或者您可以使用界面生成器@IBAction 编辑已更改

    Swift 2 版本:

    @IBAction func textChanged(sender: UITextField) {
        sender.text = sender.text?.lowercaseString
    }  
    

    【讨论】:

    • 这不是最好的方法。最好实现textField:shouldChangeCharactersInRange:replacementString: 方法并在发生更改时更新每个更改。
    • 只有在文本出现在框中才会触发,而不是令人愉快的用户体验。
    • 我的方法实际上是在用户键入时强制使用小写。
    • 奇怪..我在返回 BOOL 之前就这样做了,但它没有更新。如果您愿意,请发布答案。
    • 这种做法的问题是,如果用户在文本中间输入了一个字符,就会将选择点跳到文本的末尾。
    猜你喜欢
    • 2018-04-04
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2017-03-21
    相关资源
    最近更新 更多