uitextView contentsize在粘贴ios之后不更新

发布于 2025-01-29 09:03:43 字数 468 浏览 1 评论 0原文

当我粘贴一些文本时,contententsize的文本视图高度不正确。它在粘贴之前返回文本高度。

例如,

我期望高度值:[contentsize height] 1019(粘贴后) 但是实际价值:[contentsize height] 540(粘贴之前)

我尝试了 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 I paste some text, textview height of contentSize is not correct. 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)

I tried setNeedDisplay() method, but it's not working.

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)")

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2025-02-05 09:03:43

textView(_:syredchangeTextin:替换text:)被称为文本尚未应用于视图时,这意味着内容化尚未更改。如果可能的话,您可以使用textViewDidchange(_:),它应该打印正确的大小。

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

如果您确实需要在shordchangetextin方法上计算文本的大小,则可以尝试使用这个

When textView(_:shouldChangeTextIn:replacementText:) is called the text hasn't been applied to the view yet which means the contentSize has not changed. If possible, you can use textViewDidChange(_:), it should print the correct size.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文