简体   繁体   English

TextView第一次不能正确设置文本格式,但是第二次可以

[英]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
}

图片2 图片1

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.

相关问题 为什么GestureRecognizer第一次在textview上不起作用? - why does GestureRecognizer not work on textview first time? 我第二次专注于Textview时,文本在textView中向上移动 - Text shifted up in the textView while i focus on Textview second time ios 上的音频不能正常工作,只能在第一次工作 - Audio on ios does not work properly, it only works the first time WkWebView 消息处理程序第一次不返回文本框的值,但它在我第二次按下按钮时返回 - WkWebView Message handler doesn't return the value of textbox first time, but it does the second time I press the button Xcode:我的用户位置在第一次运行模拟时没有显示,但是在第二次运行时显示 - Xcode: My user location doesn't show the first time I run the simulation but the second time it does MPMoviePlayerPlaybackDidFinishNotification中的Segue直到第二次才触发 - Segue in MPMoviePlayerPlaybackDidFinishNotification does no fire till second time 带有NSFetchedResultsController的UITableView不会再次加载 - UITableView with NSFetchedResultsController Does Not Load the Second Time mapViewWillStartLoadingMap方法没有第二次调用Swift - mapViewWillStartLoadingMap method does not called second time Swift API 调用第二次与组合不起作用 - API call does not work for the second time with combine UIWebView stringByEvaluatingJavaScriptFromString第二次不起作用 - UIWebView stringByEvaluatingJavaScriptFromString does not work second time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM