iOS - UITextView 不绘制文本?
我的屏幕上有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我有同样的问题,我的 UITextView 不会绘制文本,直到我尝试滚动它或其父级。 [textView setNeedsDisplay] 对我不起作用。最后,我将textView增加1px,问题就消失了。
我知道这个方法很难看,但确实有效。
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.
I know this method is ugly but it does work.
系统中的大多数东西在隐藏时不会显示,这会浪费资源。
正确的方法是在 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.
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.同样的问题。我们有一个文本视图,已配置并在隐藏或屏幕外时放入视图中。很可能是由于 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 aUITextView
doing layout and drawing quite often? Thus the solution above is ugly but works: add a pixel to the frame which will trigger theUITextView
to re-layout and redraw. We went with this solution.由于 UITextView 是 UIScrollView 的子类,所以我做了这样的事情:
与 @Tùng Đỗ 相同,而不改变框架......仍然看起来很丑。
Since UITextView is a subclass of UIScrollView, I did something like this:
the same as @Tùng Đỗ without changing the frame... still looks ugly.