我想在 UITextview 中编辑一些文本,但是每当键盘出现时,它就会清除以前的文本。我也想打开键盘上的先前文本。
有人知道如何在 swift 3.0 中做到这一点吗?
我正在viewdidload中设置UITextView的字符串
treatment.text = patient?.patientTreatment;
func textViewDidBeginEditing(_ textView: UITextView)
{
if textView.text != ""
{
textView.text = patient?.patientTreatment
}
}
我在这里观察到一件事,一旦键盘打开,我的文本就会消失,一旦键盘关闭,我的文本就会消失。
最佳答案
这是一个例子。
在你的viewDidLoad
中添加以下内容:
yourTextView.text = ""
并按原样使用此委托(delegate)方法:
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.text.isEmpty {
// your textView is Empty, set your Placeholder
textView.text = "Placeholder" // Use your desired String here
} else {
// your textView contains some texts, change it according to your needs
textView.text = "My Text" // Use your desired String here
}
}
关于swift - swift 中的 UITextview 编辑模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43510300/