【问题标题】:How can I manually enable or disable the Return Key on the keyboard?如何手动启用或禁用键盘上的 Return 键?
【发布时间】:2012-02-24 23:15:47
【问题描述】:

我有一个UITextView,它接收来自键盘的输入。 “自动启用返回键”复选框被选中,所以当UITextView 为空时,返回键被禁用。

我在键盘上方有一个表情符号栏,可以将表情符号添加到UITextView,但问题出在这里;当UITextView 为空时,我向UITextView 添加表情符号,如下所示:

[inputTextView insertText:@"\ue40d"];

那么 ReturnKey 仍然被禁用,虽然 UITextView 的 text 属性不为空。

我在插入表情后试过这个:

[inputTextView setEnablesReturnKeyAutomatically:NO];

没有结果。看起来启用/禁用键盘的返回键只能通过通过键盘输入字符来触发。

知道如何手动启用/禁用 Return 键吗?

【问题讨论】:

  • 如果您在其中输入普通文本,它会按预期工作吗?

标签: objective-c ios keyboard


【解决方案1】:

这有点小技巧,但您可以尝试通过粘贴板插入文本:

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];

【讨论】:

    【解决方案2】:

    你可以使用

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

    里面的狐狸例子

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

    【讨论】:

      【解决方案3】:

      取消选中“自动启用返回键”

      【讨论】:

        猜你喜欢
        • 2016-05-22
        • 2018-08-08
        • 2015-11-20
        • 1970-01-01
        • 1970-01-01
        • 2015-05-17
        • 2013-11-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多