如何更改 Xcode 键盘上按钮 "return" 的操作?
How can i change action of the button "return" on the keyboard on Xcode?
我需要更改键盘上按钮 "return" 的文本,我什至需要更改其操作。
动作应该是一个新的段落。
你能帮帮我吗?
UITextField 具有您可以实现的委托方法,当您在键盘上按 return 时会调用它:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
您可以向 textView 文本添加一个新行字符“\n”,它将转到下一行。
UITextView
它应该像那样工作,当您按 Return.
时它会换行
您不能将 Return 按钮重命名为任何自定义文本
return 键可以采用的一些默认值是
typedef enum : NSInteger {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
} UIReturnKeyType;
默认为 "return" 其他为 Go,Google,Yahoo 为原样。
看here.
要捕获 return 事件,您可以使用 textView 委托方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
NSLog(@"Pressed Return Key");
} else {
NSLog(@"Pressed Any Other Key");
}
return YES;
}
点击并在此处设置您的关键字
[textField setReturnKeyType:UIReturnKeyDone];
对于键盘,在您的 class 中实施协议,设置
textfield.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// write you code here what you want .
return YES;
}
如果你想使用 UITextView,请参考这个 How to dismiss keyboard for UITextView with return key?
这是在键盘上添加自定义按钮的方法。但是苹果可能会拒绝你的应用,所以要小心。
- (void)viewWillAppear:(BOOL)animtated {
// Register the observer for the keyboardWillShow event
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animtated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification *)notification {
// create custom buttom
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(self.view.frame.size.width- 100, self.view.frame.size.height - 200.0f, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton addTarget:self action:@selector(addParaGraph:) forControlEvents:UIControlEventTouchUpInside];
[doneButton setBackgroundColor:[UIColor redColor]];
[doneButton setTitle:@"Add Paragraph" forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView *keyboard;
for (int i = 0; i < [tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
//if ([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) {
[keyboard addSubview:doneButton];
//}
}
}
- (void)addParaGraph:(id)sender{
// Write here you code to add new paragraph
}
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
// Set the FirstResponder of the UITextField on the layout
[theTextField resignFirstResponder];
return YES;
}
Swift 3
let addressTextField = UITextField()
addressTextField.placeholder = "Search or enter website name"
addressTextField.layer.borderColor = UIColor.gray.cgColor
addressTextField.layer.borderWidth = 1.0
addressTextField.returnKeyType = .go
view.addSubview(addressTextField)
addressTextField.delegate = self
extension YourNaneController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
我需要更改键盘上按钮 "return" 的文本,我什至需要更改其操作。 动作应该是一个新的段落。 你能帮帮我吗?
UITextField 具有您可以实现的委托方法,当您在键盘上按 return 时会调用它:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
您可以向 textView 文本添加一个新行字符“\n”,它将转到下一行。
UITextView
它应该像那样工作,当您按 Return.
您不能将 Return 按钮重命名为任何自定义文本
return 键可以采用的一些默认值是
typedef enum : NSInteger {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
} UIReturnKeyType;
默认为 "return" 其他为 Go,Google,Yahoo 为原样。
看here.
要捕获 return 事件,您可以使用 textView 委托方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
NSLog(@"Pressed Return Key");
} else {
NSLog(@"Pressed Any Other Key");
}
return YES;
}
点击并在此处设置您的关键字
[textField setReturnKeyType:UIReturnKeyDone];
对于键盘,在您的 class 中实施协议,设置
textfield.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// write you code here what you want .
return YES;
}
如果你想使用 UITextView,请参考这个 How to dismiss keyboard for UITextView with return key?
这是在键盘上添加自定义按钮的方法。但是苹果可能会拒绝你的应用,所以要小心。
- (void)viewWillAppear:(BOOL)animtated {
// Register the observer for the keyboardWillShow event
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animtated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification *)notification {
// create custom buttom
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(self.view.frame.size.width- 100, self.view.frame.size.height - 200.0f, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton addTarget:self action:@selector(addParaGraph:) forControlEvents:UIControlEventTouchUpInside];
[doneButton setBackgroundColor:[UIColor redColor]];
[doneButton setTitle:@"Add Paragraph" forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView *keyboard;
for (int i = 0; i < [tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard view found; add the custom button to it
//if ([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) {
[keyboard addSubview:doneButton];
//}
}
}
- (void)addParaGraph:(id)sender{
// Write here you code to add new paragraph
}
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
// Set the FirstResponder of the UITextField on the layout
[theTextField resignFirstResponder];
return YES;
}
Swift 3
let addressTextField = UITextField()
addressTextField.placeholder = "Search or enter website name"
addressTextField.layer.borderColor = UIColor.gray.cgColor
addressTextField.layer.borderWidth = 1.0
addressTextField.returnKeyType = .go
view.addSubview(addressTextField)
addressTextField.delegate = self
extension YourNaneController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}