iOS 键盘回车键(换行、回车符)修改
2019-03-05 本文已影响0人
山水域
UITextField 和 UITextView
如图修改键盘
UIReturnKeyDefault

UIReturnKeyGo

UIReturnKeyGoogle

UIReturnKeyJoin

UIReturnKeyNext

UIReturnKeyRoute

UIReturnKeySearch

UIReturnKeySend

UIReturnKeyYahoo

UIReturnKeyDone

UIReturnKeyEmergencyCall

UIReturnKeyContinue

UITextView *textView = [[UITextView alloc] init];
textView.returnKeyType = UIReturnKeyDone;
UITextField *textField= [[UITextField alloc] init];
textView.returnKeyType = UIReturnKeyDone;
对回车符事件进行监听
实现 UITextViewDelegate代理里面响
- -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
实现 UITextFieldDelegate代理里面响
- -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
这个函数的最后一个参数text代表你每次输入的的那个字,所以:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车,即按下return
//[textView endEditing:YES];
//在这里做你响应return键的代码
return NO; //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行
}
return YES;
}
typedef NS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
};