iPhone Force Textbox Input to Upper Case

前端 未结 11 1624
无人共我
无人共我 2020-12-15 03:01

How do I force characters input into a textbox on the iPhone to upper case?

相关标签:
11条回答
  • 2020-12-15 03:37

    The simplest way would be to , implement the editing changed method of the text field and set the textfield's text value to upper case representation of the entered text

    - (IBAction)TextFieldEditingChanged:(id)sender
    {
        _yourTextField.text = [_yourTextField.text uppercaseString];
    }
    
    0 讨论(0)
  • 2020-12-15 03:39

    I've been inspired by previous answers (thanks), but all of them had some flaws for me:

    • using textField.autocapitalizationType does not help when you also want lowercase or even more control.
    • textField.text = "my String" sets the cursor to the end of the textField which is really annoying when editing.

    So this is my Swift code that gives you full control over the typed characters (uppercase, lowercase, ignore...) AND lets the cursor where you expect it. The code is tested to work with more than one textField.

    Edit: tested with iOS 8.3

        func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
        // modify string as and if the user wants:
        // at this place you can also return false to ignore the replacement completely.
        var str = string
        if !Defaults.isUpperOrLower() {
            if Defaults.isAllLetterUpper() {
                str = string.uppercaseString
            } else {
                str = string.lowercaseString
            }
        }
    
        // updating textField.text sets the curser position to the end
        // so we store the cursor position before updating.
        let selRange = textField.selectedTextRange
    
        // Update textField as requested and modified.
        var txt = textField.text as NSString
        textField.text = txt.stringByReplacingCharactersInRange(range, withString: str)
    
        // recalculate and set the cursor position in the textField:
        // tested with
        // 1. typing a character
        // 2. backspace
        // 3. selecting a range + backspace
        // 4. selecting a range + typing a character
        if let selRange = selRange {
            var newPosition: UITextPosition?
            if range.length == 0 {
                // normal character
                newPosition = textField.positionFromPosition(selRange.end, inDirection: UITextLayoutDirection.Right, offset: count(string))
            } else {
                // backspace
                newPosition = textField.positionFromPosition(selRange.end, inDirection: UITextLayoutDirection.Left, offset: range.length - count(string))
            }
            textField.selectedTextRange = textField.textRangeFromPosition(newPosition, toPosition: newPosition)
        }
    
        // return false because we did everything manually
        return false
    }
    
    0 讨论(0)
  • 2020-12-15 03:41

    This is my code, when i have 6 textFields

    Hope you will get the point

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        if (textField==self.textField6) {
            self.textField6.text = [textField.text stringByReplacingCharactersInRange:range withString:[string uppercaseString]];
            return NO;
        }
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-15 03:44

    One issue I have with some of the above answers is if you try and set textfield.text, you will lose the cursor position. So if a user tries to edit the middle of the text, the cursor will jump to the end.

    Here is my simple Swift solution, still using UITextFieldDelegate:

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
        if textField == textFieldToUppercase {
            if string == "" {
                // User presses backspace
                textField.deleteBackward()
            } else {
                // User presses a key or pastes
                textField.insertText(string.uppercaseString)
            }
            // Do not let specified text range to be changed
            return false
        }
    
        return true
    }
    

    You still have to handle if a user presses the Return, Done, etc key on the keyboard. Just add:

    if string == "\n" {
        // Do something when 'Done' is pressed, like dismiss the keyboard
        textField.resignFirstResponder()
        return false
    }
    

    ...inside of func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool.

    0 讨论(0)
  • 2020-12-15 03:46

    return NO will have issues with back button;

    beter use this

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
        if ([[string uppercaseString] isEqualToString:string]) {
            return YES;
        }
    
        NSString *newString = [[textField.text stringByAppendingString:string] uppercaseString];
        textField.text = newString;
        return NO;
    
    }
    
    0 讨论(0)
提交回复
热议问题