菜鸟教程小白 发表于 2022-12-13 07:22:05

ios - 使用自动布局的 UITextView 动态高度 - 每个新行的文本截断顶行


                                            <p><p>我有一个扩展的“消息工具栏”,它的工作方式与消息应用程序消息栏类似,用户可以在其中键入文本,并且对于每一行新的文本,整个栏的高度都应该增加(直到它达到预定义的最大高度)。
我使用自动布局设置它,并且不断增长的消息栏工作得很好。但问题是,由于某种原因,每次我在消息栏的 UITextView 中找到新的一行文本时,顶行会因为移到 UITextView 框架的上方而略微中断。</p>

<p>这是我在用户输入消息栏时的代码:</p>

<pre><code>func messageToolbarDidUpdateText(messageToolbar: MessageToolbar, textView: UITextView)
{
    // Expand message bar to fit text view
    let newTextViewHeight = max(self.messageToolbarMinHeight - messageToolbar.textViewTopPadding - messageToolbar.textViewBottomPadding, min(self.messageToolbarMaxHeight - messageToolbar.textViewTopPadding - messageToolbar.textViewBottomPadding, textView.contentSize.height)) // Check that message bar doesn&#39;t exceed the limits
    textView.frame.size = CGSizeMake(textView.frame.width, newTextViewHeight)

    let newMessageBarHeight = max(self.messageToolbarMinHeight, min(self.messageToolbarMaxHeight, textView.contentSize.height + messageToolbar.textViewTopPadding + messageToolbar.textViewBottomPadding)) // Check that message bar doesn&#39;t exceed the limits
    self.messageToolbar.heightConstraint.constant = newMessageBarHeight

    self.view.updateConstraintsIfNeeded()
}
</code></pre>

<p>这里是我的意思的截图。请注意第一行文本之前 textview 顶部的第一张图像中的间隙。这预计会一直存在,直到消息栏达到最大高度并开始要求在 textview 中滚动新行。但是由于某种原因,即使消息栏仍未达到最大高度,新行也会导致文本的顶行被截断。它也不仅仅是滚动偏移量,因为 TextView 在消息栏达到其最大高度之前不允许滚动:
<img src="/image/sBLOY.png" alt="enter image description here"/>
<img src="/image/xWcpw.png" alt="enter image description here"/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>哦,很抱歉发布了 necroposting,但我遇到了同样的问题,终于找到了解决方案。 </p>

<p>所以基本上你需要:</p>

<pre><code>if textView.contentSize.height &lt;= textView.height {
    textView.scrollRangeToVisible(NSMakeRange(0, 0))
}
</code></pre>

<p>如果您没有达到最大工具栏高度,它会将文本滚动到正确的位置。如果你到达它,那么你的 TextView 内容大小高于它的高度,你不应该有这个问题。</p>

<p>编辑:归功于
<a href="https://stackoverflow.com/a/19047464/1316040" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/19047464/1316040</a>提到 HPGrowingTextView。
<a href="https://github.com/HansPinckaers/GrowingTextView" rel="noreferrer noopener nofollow">https://github.com/HansPinckaers/GrowingTextView</a>对于...嗯,现有的
<a href="https://github.com/KennethTsang/GrowingTextView" rel="noreferrer noopener nofollow">https://github.com/KennethTsang/GrowingTextView</a>对于实际的代码行 scrollRangeToVisible</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用自动布局的 UITextView 动态高度 - 每个新行的文本截断顶行,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/30406876/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/30406876/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用自动布局的 UITextView 动态高度 - 每个新行的文本截断顶行