Uppercase characters in UItextfield

前端 未结 17 1060
萌比男神i
萌比男神i 2020-12-24 04:34

I have a question about iOS UIKeyboard.

I have a UITextField and I would to have the keyboard with only uppercase characters.<

17条回答
  •  醉梦人生
    2020-12-24 05:35

    Set your textfield type autocapitalizationType to UITextAutocapitalizationTypeAllCharacters on the UITextField

    self.yourTexField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
    

    After call delegate

    // delegate method

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSRange lowercaseCharRange = [string rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]];
    
        if (lowercaseCharRange.location != NSNotFound) {
            textField.text = [textField.text stringByReplacingCharactersInRange:range
                                                                     withString:[string uppercaseString]];
            return NO;
        }
    
        return YES;
    }
    

提交回复
热议问题