iOS 键盘输入小写转变为大写

1。- (void)textFieldDidEndEditing:(UITextField *)textField{
textField.text = [textField.text uppercaseString];
}
优点:简单易懂
缺点:在编辑完成后才会统一将小写转换成大写
2.- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (string.length == 0 ){return YES;}
char commitChar = [string characterAtIndex:0];
if (commitChar > 96 && commitChar < 123){
NSString * uppercaseString = string.uppercaseString;
NSString * str1 = [textField.text substringToIndex:range.location];
NSString * str2 = [textField.text substringFromIndex:range.location];
textField.text = [NSStringstringWithFormat:@"%@%@%@",str1,uppercaseString,str2].uppercaseString;
return NO;
}
return YES;
}
优点:每输入一个字符就会转换一个字符的大写,完美完成需求
缺点:return NO后,textField.text 不会被保存起来 只能显示,也就是return NO之后,不会再调用- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;的action方法,所以需要在return NO之后再次调用一下action方法。

你可能感兴趣的:(iOS 键盘输入小写转变为大写)