[英]TextView does not format text properly the first time, but does the second time
I am working on a messaging app and am trying to make a UITextView that adjusts based on input. 我正在开发一个消息传递应用程序,并试图制作一个可以根据输入进行调整的UITextView。 When I add text to the view and it indents, it does not format properly (shown in image 1 below and represented by the third function of my code).
当我向视图中添加文本并使其缩进时,它的格式不正确(如下面的图像1所示,并由我的代码的第三个功能表示)。 Strangely, when I dismiss and reopen the keyboard, it formats as desired (this can be attributed to the first function of my code and is shown in image 2).
奇怪的是,当我关闭并重新打开键盘时,它会按要求设置格式(这可以归因于代码的第一个功能,如图2所示)。 Any help?
有什么帮助吗? Also, to clarify, the "..." in my code is necessary for an unrelated step I am going to carry out later (though I left it in as it may affect the code).
另外,为澄清起见,我的代码中的“ ...”对于以后将要执行的无关步骤是必需的(尽管我将其保留下来,因为这可能会影响代码)。 My code:
我的代码:
@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
}
you need to provide more information about it but if you want to pre-charge any type of information or functions before the screen start. 您需要提供有关它的更多信息,但是如果您想在屏幕启动之前对任何类型的信息或功能进行预充电。 you could use
你可以用
override func viewWillAppear(_ animated: Bool) {
//add want ever you want to do
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.