1

i need to change the text of the button "return" on my keyboard,and i even need to change its action. The action should be a new paragraph. Can you help me?

5 Answers5

2

You cannot rename the Return button to any custom text

Some default values thatThe return key can take are

typedef enum : NSInteger {
   UIReturnKeyDefault,
   UIReturnKeyGo,
   UIReturnKeyGoogle,
   UIReturnKeyJoin,
   UIReturnKeyNext,
   UIReturnKeyRoute,
   UIReturnKeySearch,
   UIReturnKeySend,
   UIReturnKeyYahoo,
   UIReturnKeyDone,
   UIReturnKeyEmergencyCall,
} UIReturnKeyType;

where Default is "return" others are Go, Google, Yahoo as is.

look here.

And for capturing the return Event you can use the textView Delegate method

- (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;
}
Bonnie
  • 4,943
  • 30
  • 34
0

UITextField has this delegate method that you can implement, and get's called when you press return on the keyboard: - (BOOL)textFieldShouldReturn:(UITextField *)textField You can add a new line char "\n" to the textView text and it would go to next line.

UITextView it's suppose to work like that where it would go to a new line when you press Return.

pteofil
  • 4,133
  • 17
  • 27
0

enter image description here

click and set your keyword here

 [textField setReturnKeyType:UIReturnKeyDone];

for keyboard implement the protocol in your class, set

textfield.delegate = self;


  - (BOOL)textFieldShouldReturn:(UITextField *)textField {

    // write you code here what you want .

        return YES;
    }

refer this if you want to UITextView How to dismiss keyboard for UITextView with return key?

Community
  • 1
  • 1
Sport
  • 8,570
  • 6
  • 46
  • 65
0

Here is the way to add custom button over the keyboard. But apple could reject your app so be careful.

- (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;
}
abhishekkharwar
  • 3,529
  • 2
  • 18
  • 31
0

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
    }
}
Giang
  • 3,553
  • 30
  • 28