![](/img/trans.png)
[英]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.