ios - 在 UITextView 中快速将 'Return' 按钮功能更改为 'Done'

标签 ios iphone swift cocoa-touch

我想在用户打字时摆脱键盘的“返回”功能,因此没有新行,所以我希望“返回”键的功能与“完成”一样隐藏键盘。

我使用的是可编辑的 UITextView,因此用户可以输入他们的帖子,并将其发布到主时间轴,但由于我有固定的单元格,我不希望用户能够按'返回',他们的帖子将超出时间线的范围。

我发现它适用于 UITextField,但不适用于 UITextView:

func textFieldShouldReturn(textField: UITextField!) -> Bool {
    textField.resignFirstResponder()  //if desired
    return true
}

所以我只想知道在 UITextView 中是否有办法做到这一点,或者至少能够在按下返回时隐藏键盘,而不是创建一个新行。

最佳答案

可以设置文本域的返回键类型:

textField.returnKeyType = UIReturnKeyType.done

更新 如上所述,您绝对可以使用相同的方法将返回键设置为“完成”。但是,当用户按下返回键时,UITextView 不提供回调。作为解决方法,您可以尝试处理 textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) 委托(delegate)调用,并在检测到换行符输入时关闭键盘:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if (text == "\n") {
        textView.resignFirstResponder()
    }
    return true
}

关于ios - 在 UITextView 中快速将 'Return' 按钮功能更改为 'Done',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31886720/

相关文章:

Swift 扩展和 'Element' 显式初始化

ios - 在 Swift 中使用 Firebase 中的句柄删除观察者

ios - NSDate 奇怪的小时

iphone - 如何在后台运行的应用程序中检测 iphone 电池电量?

iphone - 如何从 View 中动态隐藏按钮?

iphone - iphone sdk中的ffmpeg问题?

ios - 避免多个 if 子句调用不同的方法

iphone - UINavigationController 中的 Cocos2d 项目

ios - 从其他类传递和获取数据时显示 null

c - Swift:UnsafeMutablePointer.deallocate(capacity:) 与 free() 的互操作性