UITextField autocapitalizationType UITextAutocapitalizationTypeAllCharacters not working on device

喜夏-厌秋 提交于 2019-12-22 05:06:25

问题


You can set the autocapitalizationType property of a UITextField so all input is in upper case. I find that does work fine on the simulator (when actually tapping the simulator's keypad, not the Mac's keyboard), but not on the device? Everything stays lowercase.

In the UICatalog demo I added to the textFieldNormal method:

textFieldNormal.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

Added a delegate too, to display the actual autocapitalizationType for the UITextField:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog( @"textField.autocapitalizationType=%d", textField.autocapitalizationType );
}

It will properly display 3 (=UITextAutocapitalizationTypeAllCharacters), but anything you tap remains lowercase. What am I missing?


回答1:


Apparently this is a device general settings issue: Settings -> General -> Keyboard -> Auto-Capitalization must be ON to honour the setting of textField.autocapitalizationType to all upper case, else setting the property is ignored, apparently. If I switch it on everything works as expected.




回答2:


You could try something like this the the textfield delegate:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.length == 0) { // not deleting , but adding a char        
        textField.text = [textField.text stringByAppendingString:[string uppercaseString]];
        return NO;
    }

    return YES;
}

This works only if you try to insert a symbol at the end of the text. Should you want to play with the text in the middle you could play with

range.location

and also you will need to play with the cursor positioning as it will go at the end every time...

I hope this helps someone.



来源:https://stackoverflow.com/questions/17879752/uitextfield-autocapitalizationtype-uitextautocapitalizationtypeallcharacters-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!