高度达到8192.0后,UITextView文本变为不可见 [英] UITextView text becomes invisible after height reaches 8192.0

查看:78
本文介绍了高度达到8192.0后,UITextView文本变为不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIScrollView 中嵌入了不可滚动的 UITextView ,并将文字添加到 UITextView 动态。 UIScrollView 根据TextView的框架相应地调整它的contentSize。但是,一旦 UITextView 超过8192的高度,文本将变为不可见(但仍然存在,因为您可以使用放大镜突出显示文本,甚至可以看到部分文本文本通过放大镜)。

I have a non-scrollable UITextView embedded in a UIScrollView and add text to the UITextView dynamically. The UIScrollView adjust it's contentSize accordingly based on the TextView's frame. However, once the UITextView exceeds a height of 8192, the text will become invisible (but still there, because you can use the magnifying glass to highlight text and even see parts of the text through the magnifying glass).

  CGRect textviewFrame = self.TextView.frame;
  textviewFrame.size.height = [self textViewHeightForAttributedText:self.TextView.attributedText andWidth:320.0];
  self.TextView.frame = textviewFrame;
  self.ScrollView.contentSize = self.TextView.frame.size;

帮助函数大小 UITextView 相应:

- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
  UITextView *textView = [[UITextView alloc] init];
  [textView setAttributedText:text];
  CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
  return size.height;
}

没有意识到这是同一个未解决的问题此处直到我通过强制将最大大小强制为8193并且出现问题(最大大小为8192)来明确测试它仍然正确显示文字)。有人在遇到这个问题之前就知道了解决这个问题吗?谢谢

Didn't realize it was the same exact problem that was unsolved here until I tested it out explicitly by forcing the max size to 8193 and the problem occurred (while a max size of 8192 still had the text showing correctly). Anyone run into this problem before and know of a work around? Thanks

推荐答案

我最近遇到了这个问题并且已经找到了有效的方法。虽然它似乎是一个iOS的错误恕我直言,但它确实没有... CALayer尺寸有实际限制,加上绘制一个8K高的文本需要很长时间。更好的做法就像Apple想要的那样,只渲染出一些可见的文本...这就是为什么UITextView毕竟扩展了UIScrollView。

I was recently hit by this problem and have worked out an effective way around it. Although it seems like an iOS bug IMHO it's really not... there are practical limits to CALayer sizes, plus drawing an 8K high piece of text takes a long time. Much better to do as Apple intended and to only render the bit of text that's visible... that's why UITextView extends UIScrollView after all.

问题是UITextView不是'非常容易与其他UI集成。在我的情况下,我正在开发一个新闻应用程序,其中使用单个UITextView来呈现文章,并且在其上方和下方有一些单独的UI(标题和按钮等),所有这些都托管在一个可滚动的容器中。

The problem is that UITextView isn't terribly easy to integrate with other bits of UI. In my case I am working on a news app where a single UITextView is used to render the article, plus there's some separate UI (titles and buttons etc) above and below it, all hosted in a single scrollable container.

我发现的解决方案是将UITextView分成两个视图...一个专用的UITextView容器,其框架是全文大小(即UITextView的contentSize大小相同),是UITextView的超级视图。你的孩子UITextView的框架应该设置为外部可滚动容器的边界。

The solution I've found is to split the UITextView into two views... a dedicated UITextView container whose frame is the full text size (i.e. the same size your UITextView's contentSize) and which is the superview of your UITextView. Your child UITextView's frame should be set to the bounds of the outer scrollable container.

然后你要做的就是使用键值观察监视你的contentOffset属性外部可滚动容器视图(在我的例子中,这是一个UICollectionView)。当其contentOffset更改时,您更新(1)UITextView的contentOffset,以及(2)UITextView图层的transform属性。通过更新该转换属性,UITextView被修复以填充其超级视图的当前可见部分。但是因为你也在更新UITextView的contentOffset,这个技巧对于用户来说是完全不可见的...它的外观和行为就好像UITextView非常大。

Then all you have to do is use key-value observation to monitor the contentOffset property of your outer scrollable container view (in my case this is a UICollectionView). When its contentOffset changes you update (1) the contentOffset of your UITextView, and (2) the transform property of the UITextView's layer. By updating that transform property the UITextView is fixed to fill the currently-visible part of it's superview. But because you're also updating the UITextView's contentOffset, this trickery is totally invisible to the user... it looks and behaves as if the UITextView is simply very large.

这篇关于高度达到8192.0后,UITextView文本变为不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆