iOS - UITextView 不绘制文本?

发布于 2024-11-30 01:57:47 字数 340 浏览 0 评论 0原文

我的屏幕上有一个 UITextView。 此 UITextView 被 UIView 覆盖。

当我为此 UITextView 设置文本时,如果 textView 不可见(UIView 覆盖它),它不会绘制文本。

因此,当我删除覆盖我的 textView 的视图时,我的 textView 中没有文本。如果我尝试滚动文本视图,文本会突然出现。

有什么方法可以强制 UITextView 始终绘制文本吗? (只有当我的文本太多(大约 1000 个字符)时才会发生这种情况)

我尝试在设置“文本”之前和之后调用 [textView setNeedsDisplay],但它没有解决问题

I have a UITextView on the screen.
This UITextView is covered by a UIView.

When I set the text for this UITextView, if the textView is not visible (UIView is covering it), it doesn't draw the text.

So when I remove the view that is covering my textView there is no text in my textView. If I try scrolling the textview the text will appear suddenly.

any way to force the UITextView to draw the text at all times? (This only happens when I have too much text in it (about 1000 characters))

I tried calling [textView setNeedsDisplay], before and after setting 'text' and it didn't fix the problem

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

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

发布评论

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

评论(5

少女七分熟 2024-12-07 01:57:47

我有同样的问题,我的 UITextView 不会绘制文本,直到我尝试滚动它或其父级。 [textView setNeedsDisplay] 对我不起作用。最后,我将textView增加1px,问题就消失了。

CGRect frame = textView.frame;
frame.size.height += 1;
textView.frame = frame;

我知道这个方法很难看,但确实有效。

I have the same problem, my UITextView does not draw text until I try scrolling it or its parent. [textView setNeedsDisplay] does not work for me. Finally, I increase textView 1px and the problem's gone.

CGRect frame = textView.frame;
frame.size.height += 1;
textView.frame = frame;

I know this method is ugly but it does work.

凤舞天涯 2024-12-07 01:57:47

系统中的大多数东西在隐藏时不会显示,这会浪费资源。

正确的方法是在 UITextView 取消隐藏后调用 setNeedsDisplay: ,然后它应该立即绘制。

Most things in the system will not display when hidden, which would be a waste of resources.

The correct approach is to call setNeedsDisplay: on the UITextView AFTER it has been un-hidden, then it should draw immediatley.

眼眸印温柔 2024-12-07 01:57:47

UITextView 中的字符数限制似乎相对较低(请参阅此处的帖子)。

我发布的链接中的海报建议将您的内容放入 UIWebView,可以处理大量文本。

The limit to the number of characters in a UITextView seems to be around relatively low (see the post here).

The poster in the link I posted suggests putting your content into a UIWebView, which can handle a large amount of text.

謌踐踏愛綪 2024-12-07 01:57:47

同样的问题。我们有一个文本视图,已配置并在隐藏或屏幕外时放入视图中。很可能是由于 Apple 优化了 UITextView,因此它不会进行布局和绘图,除非它在屏幕上且可见。

由于优化,调用 setNeedsDisplay 不起作用。 setNeedsDisplay 经常被调用,那么想象一下 UITextView 经常进行布局和绘制吗?因此,上面的解决方案虽然丑陋但有效:向框架添加一个像素,这将触发 UITextView 重新布局和重绘。我们采用了这个解决方案。

Same problem. We have a textview that is configured and put into the view while hidden or offscreen. Most likely due to Apple optimizing UITextView so that it doesn't do layout and drawing unless it is onscreen and visible.

Calling setNeedsDisplay doesn't work because of the optimizations. setNeedsDisplay gets called quite often, so imagine a UITextView doing layout and drawing quite often? Thus the solution above is ugly but works: add a pixel to the frame which will trigger the UITextView to re-layout and redraw. We went with this solution.

不离久伴 2024-12-07 01:57:47

由于 UITextView 是 UIScrollView 的子类,所以我做了这样的事情:

CGPoint offset = self.textView.contentOffset;
offset.y++;
[self.textView setContentOffset:offset];
offset.y--;
[self.textView setContentOffset:offset];

与 @Tùng Đỗ 相同,而不改变框架......仍然看起来很丑。

Since UITextView is a subclass of UIScrollView, I did something like this:

CGPoint offset = self.textView.contentOffset;
offset.y++;
[self.textView setContentOffset:offset];
offset.y--;
[self.textView setContentOffset:offset];

the same as @Tùng Đỗ without changing the frame... still looks ugly.

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