gpt4 book ai didi

ios - TextView 第一次没有正确格式化文本,但第二次可以

转载 作者:行者123 更新时间:2023-11-28 07:27:34 26 4
gpt4 key购买 nike

我正在开发一个消息传递应用程序,并试图制作一个可根据输入进行调整的 UITextView。当我向 View 添加文本并缩进时,它的格式不正确(如下图 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
}

Image2 Image 1

最佳答案

您需要提供更多相关信息但是如果你想在屏幕启动之前预充电任何类型的信息或功能。你可以使用

override func viewWillAppear(_ animated: Bool) {
//add want ever you want to do
}

关于ios - TextView 第一次没有正确格式化文本,但第二次可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56010817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com