我有两个问题,我是swift的初学者
首先,我想在用户输入第一个字符之前隐藏发送按钮。我使用文本字段委托(delegate)函数,但它们没有实现这个想法。
然后,当用户在文本字段中输入时,我想创建一个空白行。我知道使用“textFieldShouldReturn”函数,但我使用“\n”,它对我不起作用。如何做到这一点?< br/>
这是我的代码:
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.text?.isEmpty == false {
aButton.isHidden = true
sendButton.isHidden = false
}
}
更新代码:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.textField.text = "\n"
return true
}
我还希望文本字段更改它的高度。让用户可以输入他们的消息。能否实现?
或者我需要将文本字段更改为 TextView 。
最佳答案
您可以使用文本字段委托(delegate)方法来检查用户何时开始在您的文本字段中写入内容。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
if textField == txtUserName //compare with your textfield object which you have taken by outlet
{
if string.characters.count >= 1 {
//make your button enable
}else
{
// disable your button
}
}
return true
}
关于ios - 什么功能可以帮助我了解用户在文本字段中输入的第一个字符以及当我按下键盘返回时如何制作一个空行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43088264/