UITextView滚动-人为限制

发布于 2024-08-31 04:33:00 字数 114 浏览 2 评论 0原文

我有一个 UITextView,高度为 300。我想要的是当键入的文本到达中间点时,滚动开始,就像它在 textView 的底部一样。基本上我想以编程方式设置 textView 中滚动开始的点。有什么想法吗?谢谢。

I have a UITextView with a height of let's say 300. What I would like is when the typed text gets to the half way point, for the scrolling to start as if it were at the bottom of the textView. Basically I would like to programmatically set the point within the textView for scrolling to begin. Any ideas? Thanks.

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

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

发布评论

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

评论(2

痴意少年 2024-09-07 04:33:00

您想要这样做是因为键盘导致文本隐藏在其后面吗?如果是这样,只需在键盘抬起时缩小文本视图的大小(可能是动画的,以便随着键盘的增大而缩小),然后让系统负责滚动。

如果您尝试使文本以与默认方式不同的方式滚动,也许您应该重新考虑,对于习惯了文本滚动达到“正常”级别的人来说,它看起来有点奇怪。

但是,如果您必须这样做,也许您可​​以使用在 UITextView 引发后添加为子视图的 UIImageView - 将其视为 UITextView 的非活动扩展。制作与 UITextView 宽度相同的图像,使其高度适合您的要求,并将其放置在覆盖 UITextView 下边缘的位置。看起来文本视图比实际的要长,但是当文本接近其实际边框(而不是假边框)时,文本视图的滚动将会起作用。

我在 UITextView/UIScrollView/UIScrollViewDelegate 中看不到任何执行此操作的内容。

Do you want to do this because the keyboard is causing the text to be hidden behind it? If so just size the textview smaller when the keyboard is raised (possibly animated so that it shrinks as the keyboard grows) then let the system take care of scrolling.

If you are trying to make the text scroll in a different way to the default maybe you should reconsider, it will look a bit strange to people who are used to it scrolling when it reaches the "normal" level.

However if you must do it maybe you could use a UIImageView added as a subview after the UITextView is raised - consider it as an inactive extension of the UITextView. Make an image of the same width as the UITextView, make it the right height for your requirements and position it so that it covers the lower edge of the UITextView. It will look like the textview is longer than it is but the textview's scrolling will work when text nears its actual border, not your fake one.

I can't see anything in UITextView/UIScrollView/UIScrollViewDelegate to do this.

白云悠悠 2024-09-07 04:33:00

谢谢,亚当。我确实这样做是因为键盘导致文本隐藏在后面,我稍后会讲到。

您对 UIImageView 的建议正是我的计划,但相反。我想要 300 高度的 textView,其下部被键盘覆盖,然后在 textView 所在的分组 tableView 的圆形底部部分的键盘上方有一个 UIImageView 条子。(textView 位于单个 tableViewCell 中)部分/单单元格 tableView。)添加图像的条子将使较大的 textView(被键盘覆盖)在键盘存在时看起来像一个小 textView,并且当键盘关闭并且图像被删除/恢复时,textView 会更大,以允许用户阅读他们输入的所有注释。

通过我正在寻找的解决方案,滚动到 textView 内的任何位置停止,较小的 textView 将在正确的位置开始滚动,就在键盘上方的条子图像上方。

现在提出“调整 textView 的大小”建议。说起来容易做起来难。因为 textView 位于分组的 tableView 中,并且存在键盘,所以我在 tableViewCell 中创建了一个较小的 textView,具有漂亮的圆角,但是当我将 textView 调整得较大时,只有中心部分延伸(大约是中心圆角矩形的 90%) ),而不是侧面的圆角(每侧宽度的 5% 保持不变)。接下来的想法:在不增加单元格高度的情况下无法扩展textView,所以……

解决方案是调整单元格大小;但这涉及 reloadData 或开始/结束更新,仅适用于单元格或部分插入。然后我正在考虑添加一个新部分,以便 reloadData 可以工作,但随后我必须关闭滚动视图上的滚动,这样新部分就永远不会被看到,尽管尝试了每种排列,但我还是遇到了麻烦scrollEnabled=NO(在 tableView、textView、scrollView、父级上)。

最后,向上移动整个屏幕效果很好,但是对于一个大的 textView,我必须防止在向上滚动时输入不可见的上部部分,因此被拒绝。尽管我刚刚想到,随着 CGRect 的 size.height 同样减小,打字可能不会发生在不可见部分 - 但我仍然失去了分组 tableView 的圆形顶部,因此从外观上看它可能看起来更好 - 仍然被拒绝。

这就是我的故事,这就是我的一天。任何人的任何帮助将不胜感激。

我认为我最好的选择是将“假”部分添加到 tableView 并将其设置为隐藏,这样我就可以启动新的单元格高度。但我仍然必须关闭 tableView 滚动或scrollView 滚动,以便在textView 不会不必要地移动整个屏幕,因为隐藏部分的空间可能在那里,因此可以滚动。

Thanks, Adam. I am indeed doing this because the keyboard is causing text to be hidden behind and I'll get to that in a sec.

What you suggest with respect to the UIImageView is exactly what I was planning but in reverse. I wanted the 300 height textView, with the lower portion covered by the keyboard to then have a UIImageView sliver just above the keyboard of the rounded bottom portion of the grouped tableView that the textView is in. (The textView is in a tableViewCell in a single section/single cell tableView.) Adding the sliver of an image would make the larger textView, covered by the keyboard look like a small textView when the keyboard is present, and when dismissed and image removed/restored, textView would be larger, to allow the user to read all of the notes they've entered.

With the solution that I was looking for, scrolling to stop anywhere within the textView, the smaller textView would start scrolling at the correct place, just above the sliver image which is just above the keyboard.

Now to the "just size the textView" suggestion. Easier said than done. Because the textView is in a grouped tableView, with keyboard present, I created a smaller textView in a tableViewCell with nicely rounded corners, but when I size the textView larger, only the center portion extends (about 90% of the rounded rectangle in the center), not the rounded corners on the sides (5% of the width on each side remains the same). Next thought: Can't extend the textView without increasing the height of the cell so...

...the solution is therefore to resize the cell; but that involves a reloadData or begin/end updates which only work with a cell or section insert. I was then thinking of just adding a new section so the reloadData would work, but then I'd have to turn off scrolling on the scrollView so the new section would never be seen and I'm having trouble with that despite trying every permutation of scrollEnabled=NO (on tableView, textView, scrollView, parent).

And finally, shifting the entire screen upward works beautifully but with a large textView, I'd then have to prevent typing in the non-visible upper portion when scrolled upward, so that was rejected. Although it just occurred to me that with the CGRect's size.height equally reduced, typing may not occur in the non-visible portion - but I still lose the rounded top of the grouped tableView so cosmetically it could look better - still rejected.

So, that's my story and that's been my day. Any help from anyone would be greatly appreciated.

I think my best bet is adding the "fake" section to the tableView and setting it to hidden, so I can get the new cell height to kick in. but I still have to turn off tableView scrolling or scrollView scrolling so a scroll in the textView doesn't unnecessarily move the entire screen since the space for the hidden section would probably be there and therefore scrollable.

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