[英]why does GestureRecognizer not work on textview first time?
[英]TextView does not format text properly the first time, but does the second time
我正在开发一个消息传递应用程序,并试图制作一个可以根据输入进行调整的UITextView。 当我向视图中添加文本并使其缩进时,它的格式不正确(如下面的图像1所示,并由我的代码的第三个功能表示)。 奇怪的是,当我关闭并重新打开键盘时,它会按要求设置格式(这可以归因于代码的第一个功能,如图2所示)。 有什么帮助吗? 另外,为澄清起见,我的代码中的“ ...”对于以后将要执行的无关步骤是必需的(尽管我将其保留下来,因为这可能会影响代码)。 我的代码:
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.textView.frame.origin.y -= keyboardSize.height
self.send.frame.origin.y -= keyboardSize.height
let number = textView.contentSize.height / textView.font!.lineHeight
textView.frame.size.height = CGFloat(42 + Double((Int(number) - 1))*20.3)
textView.frame.origin.y -= CGFloat(Double((Int(number) - 1))*20.3)
textView.text = "................................................................................."
textView.text = textMessage
textView.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.2).cgColor
textView.alpha = 1
send.backgroundColor = UIColor(red: 0.08627, green: 0.5373, blue: 1, alpha: 1)
}
}
@objc func keyboardWillHide(notification: NSNotification) {
self.textView.frame.origin.y = 619
self.send.frame.origin.y = 623
textView.frame.size.height = 42
textMessage = textView.text
}
func textViewDidChange(_ textView: UITextView) {
let number = textView.contentSize.height / textView.font!.lineHeight
print(textView.font!.lineHeight)
new = Int(number)
if new != old {
textView.frame.size.height = CGFloat(42 + Double((Int(number) - 1))*20.3)
textView.frame.origin.y -= CGFloat(20.3)
}
old = new
}
您需要提供有关它的更多信息,但是如果您想在屏幕启动之前对任何类型的信息或功能进行预充电。 你可以用
override func viewWillAppear(_ animated: Bool) {
//add want ever you want to do
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.