Bas Holtrop
Bas Holtrop

Reputation: 91

How can I manually enable or disable the Return Key on the keyboard?

I have a UITextView that receives input from the keyboard. The "Auto Enable Return Key" checkbox is checked, so when the UITextView is empty the Return key is disabled.

I have an Emoticons bar above the keyboard that can add emoticons to the UITextView as well, but here's the problem; When the UITextView is empty and I add an emoticon to the UITextView like this:

[inputTextView insertText:@"\ue40d"];

then the ReturnKey is still disabled, although the UITextView's text property is not empty.

I've tried this after inserting an emoticon:

[inputTextView setEnablesReturnKeyAutomatically:NO];

With no results. It looks like enabling/disabling the Return Key of the keyboard is only triggered by entering characters through the keyboard.

Any idea's how to do manually enable/disable the Return key?

Upvotes: 7

Views: 2935

Answers (3)

Paul B
Paul B

Reputation: 5115

You can use

textField.setValue(isEnabled, forKeyPath: "inputDelegate.returnKeyEnabled")

Fox example inside

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool

Upvotes: 0

Paul
Paul

Reputation: 37

uncheck "Auto Enable Return Key"

Upvotes: -1

Nick Lockwood
Nick Lockwood

Reputation: 40995

It's a bit of a hack, but you could try inserting the text via the pasteboard instead:

UIPasteboard* generalPasteboard = [UIPasteboard generalPasteboard];

// Save a copy of the system pasteboard's items
// so we can restore them later.
NSArray* items = [generalPasteboard.items copy];

// Set the contents of the system pasteboard
// to the text we wish to insert.
generalPasteboard.string = text;

// Tell this responder to paste the contents of the
// system pasteboard at the current cursor location.
[textfield paste: nil];

// Restore the system pasteboard to its original items.
generalPasteboard.items = items;

// Free the items array we copied earlier.
[items release];

Upvotes: 2

Related Questions