简体   繁体   English

iOS - UITextView 不绘制文本?

[英]iOS - UITextView doesn't draw text?

I have a UITextView on the screen.我在屏幕上有一个 UITextView。 This UITextView is covered by a UIView.这个 UITextView 被 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.当我为这个 UITextView 设置文本时,如果 textView 不可见(UIView 正在覆盖它),它不会绘制文本。

So when I remove the view that is covering my textView there is no text in my textView.因此,当我删除覆盖我的 textView 的视图时,我的 textView 中没有文本。 If I try scrolling the textview the text will appear suddenly.如果我尝试滚动 textview 文本会突然出现。

any way to force the UITextView to draw the text at all times?有什么方法可以强制 UITextView 始终绘制文本? (This only happens when I have too much text in it (about 1000 characters)) (这只发生在我有太多文本(大约 1000 个字符)时)

I tried calling [textView setNeedsDisplay] , before and after setting 'text' and it didn't fix the problem我尝试在设置“文本”之前和之后调用[textView setNeedsDisplay]并没有解决问题

I have the same problem, my UITextView does not draw text until I try scrolling it or its parent.我有同样的问题,我的 UITextView 在我尝试滚动它或其父级之前不会绘制文本。 [textView setNeedsDisplay] does not work for me. [textView setNeedsDisplay] 对我不起作用。 Finally, I increase textView 1px and the problem's gone.最后,我将 textView 增加 1px,问题就消失了。

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

I know this method is ugly but it does work.我知道这种方法很难看,但确实有效。

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 上调用 setNeedsDisplay: 在它被取消隐藏之后,它应该立即绘制。

Same problem.同样的问题。 We have a textview that is configured and put into the view while hidden or offscreen.我们有一个 textview 配置并在隐藏或离屏时放入视图。 Most likely due to Apple optimizing UITextView so that it doesn't do layout and drawing unless it is onscreen and visible.很可能是由于 Apple 优化了UITextView ,因此它不会进行布局和绘图,除非它在屏幕上并且可见。

Calling setNeedsDisplay doesn't work because of the optimizations.由于优化,调用setNeedsDisplay不起作用。 setNeedsDisplay gets called quite often, so imagine a UITextView doing layout and drawing quite often? setNeedsDisplay经常被调用,所以想象一个UITextView经常做布局和绘图? Thus the solution above is ugly but works: add a pixel to the frame which will trigger the UITextView to re-layout and redraw.因此,上面的解决方案很丑但有效:向框架添加一个像素,这将触发UITextView重新布局和重绘。 We went with this solution.我们采用了这个解决方案。

Since UITextView is a subclass of UIScrollView, I did something like this:由于 UITextView 是 UIScrollView 的子类,我做了这样的事情:

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.与@Tùng Đỗ 相同,但不改变框架......仍然看起来很丑。

The limit to the number of characters in a UITextView seems to be around relatively low ( see the post here ). UITextView中字符数的限制似乎相对较低( 请参阅此处的帖子)。

The poster in the link I posted suggests putting your content into a UIWebView , which can handle a large amount of text.我发布的链接中的海报建议将您的内容放入可以处理大量文本的UIWebView中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM