简体   繁体   English

UITextView contentSize 在粘贴 swift iOS 后没有更新

[英]UITextView contentSize is not update after paste swift iOS

When I paste some text, textview height of contentSize is not correct.当我粘贴一些文本时, contentSize 的 textview 高度不正确。 It returns text height before paste.它在粘贴之前返回文本高度。

For example,例如,

I expect height value : [contentSize Height] 1019 (after paste) But real value : [contentSize Height] 540 (before paste)我期望高度值:[contentSize Height] 1019(粘贴后)但实际值:[contentSize Height] 540(粘贴前)

I tried setNeedDisplay() method, but it's not working.我尝试了 setNeedDisplay()方法,但它不起作用。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
{
    print("[contentSize] \(textView.contentSize.height) size to fit \(textView.sizeThatFits(view.frame.size).height)")

}

When textView(_:shouldChangeTextIn:replacementText:) is called the text hasn't been applied to the view yet which means the contentSize has not changed.当调用textView(_:shouldChangeTextIn:replacementText:)时,文本尚未应用于视图,这意味着 contentSize 尚未更改。 If possible, you can use textViewDidChange(_:) , it should print the correct size.如果可能,您可以使用textViewDidChange(_:) ,它应该打印正确的大小。

func textViewDidChange(_ textView: UITextView) {
    print("[contentSize] \(textView.contentSize.height) size to fit \(textView.sizeThatFits(view.frame.size).height)")
}

If you really need to calculate the size of the text on the shouldChangeTextIn method, you could try calculating the size using an approach like this .如果你真的需要在shouldChangeTextIn方法上计算文本的大小,你可以尝试使用这样的方法来计算大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM