简体   繁体   English

在 UITextView 中快速将“返回”按钮功能更改为“完成”

[英]Change 'Return' button function to 'Done' in swift in UITextView

I would like to get rid of the "return" function of the keyboard while the user is typing, so there are no new lines, so instead I would like the 'return' key to function as 'Done' so it would hide the keyboard.我想在用户打字时摆脱键盘的“返回”功能,所以没有新行,所以我希望“返回”键起到“完成”的作用,这样它就会隐藏键盘.

I am using a UITextView, that is editable, so the user is able to type their post, and post it to the main timeline, but since I have fixed cells, I don't want the user to be able to press 'return' and their post would be out of range of the timeline.我正在使用 UITextView,这是可编辑的,因此用户可以输入他们的帖子,并将其发布到主时间轴,但由于我有固定的单元格,我不希望用户能够按“返回”他们的帖子将超出时间表的范围。

I found this that works with UITextField, but not with UITextView:我发现这适用于 UITextField,但不适用于 UITextView:

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

So I just wanted to know if there is a way to do that in a UITextView, or at least to be able to hide the keyboard if pressed return, instead of creating a new line.所以我只是想知道是否有办法在 UITextView 中做到这一点,或者至少在按下返回时能够隐藏键盘,而不是创建一个新行。

You can set the return key type of the text field:您可以设置文本字段的返回键类型:

textField.returnKeyType = UIReturnKeyType.done

Update You can definitely use the same approach to set the return key to "Done", as mentioned above.更新您绝对可以使用相同的方法将返回键设置为“完成”,如上所述。 However, UITextView doesn't provide a callback when user hits the return key.但是,当用户点击返回键时,UITextView 不提供回调。 As a workaround, you can try to handle the textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) delegate call, and dismiss the keyboard when you detect the input of a new line character:作为一种解决方法,您可以尝试处理 textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) 委托调用,并在检测到新行字符的输入时关闭键盘:

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

I have tried many codes and finally this worked for me in Swift 3.0 Latest [April 2019] this achieved using UITextFields我已经尝试了很多代码,最后这在 Swift 3.0 最新 [2019 年 4 月] 中对我有用,这是使用 UITextFields 实现的

The "ViewController" class should be inherited the "UITextFieldDelegate" for making this code working. “ViewController”类应该继承“UITextFieldDelegate”以使此代码工作。

class ViewController: UIViewController,UITextFieldDelegate  

Add the Text field with the Proper Tag number and this tag number is used to take the control to appropriate text field based on incremental tag number assigned to it.添加具有正确标签编号的文本字段,该标签编号用于根据分配给它的增量标签编号将控件带到适当的文本字段。

override func viewDidLoad() {

    userNameTextField.delegate = self
    userNameTextField.tag = 0
    userNameTextField.returnKeyType = .next
    passwordTextField.delegate = self
    passwordTextField.tag = 1
    passwordTextField.returnKeyType = .go
}

In the above code, the "returnKeyType = UIReturnKeyType.next" where will make the Key pad return key to display as "Next" you also have other options as "Join/Go" etc, based on your application change the values.在上面的代码中,“returnKeyType = UIReturnKeyType.next”将使键盘返回键显示为“Next”,您还有其他选项如“Join/Go”等,根据您的应用程序更改值。

This "textFieldShouldReturn" is a method of UITextFieldDelegate controlled and here we have next field selection based on the Tag value incrementation.这个“textFieldShouldReturn”是 UITextFieldDelegate 控制的一个方法,这里我们有基于 Tag 值增量的下一个字段选择。

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
    if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
        nextField.becomeFirstResponder()
    } else {
        textField.resignFirstResponder()
        return true;
    }
    return false
}

If you're working with a storyboard or xib, you can change the UITextView's Return button to 'Done' (or various other options) within Interface Builder, without the need for any setup code.如果您正在使用故事板或 xib,您可以在 Interface Builder 中将UITextView's返回按钮更改为“完成”(或各种其他选项),而无需任何设置代码。 Just look for this option in the Attributes inspector :只需在属性检查器中查找此选项:

Interface Builder 中的 Return Key 选项

From there, you just pair it up with the UITextViewDelegate code that others have already provided here.从那里,您只需将它与其他人已经在此处提供的UITextViewDelegate代码配对。

Swift v5:斯威夫特 v5:

extension ExampleViewController: UITextViewDelegate {

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

And then, in your viewDidLoad() method:然后,在您的viewDidLoad()方法中:

exampleTextView.delegate = self

Working in Swift 4在 Swift 4 中工作

Add this in viewDidLoad().在 viewDidLoad() 中添加它。

textField.returnKeyType = UIReturnKeyType.Done

Add this anywhere you like.将此添加到您喜欢的任何位置。

extension UITextView: UITextViewDelegate {
    public func textViewDidChange(_ textView: UITextView) {
        if text.last == "\n" { //Check if last char is newline
            text.removeLast() //Remove newline
            textView.resignFirstResponder() //Dismiss keyboard
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM