在 Swift 中的 UITextfield 上按下 return 按钮时如何添加 space?

How do I add a space when pressing the return button on a UITextfield in Swift?

我有这个文本字段,用户可以在其中插入文本,我想在按下 return 按钮时自动按下 space 栏按钮。无论如何能够做到这一点?

//when user presses return the keyboard will dismiss and automatically add a space

func textFieldShouldReturn(_ searchTextField: UITextField) -> Bool {

    searchTextField.resignFirstResponder()

    return true
}

你可以试试

searchTextField.text! += " " 
searchTextField.resignFirstResponder()
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.text = "\(textField.text ?? "")_" //replace underscore with space
    return true
}

将 space 连接到文本字段上的当前文本