我在 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
的大小:
Helper function to size UITextView
accordingly:
- (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.
这篇关于UITextView 文字在高度达到 8192.0 后变得不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!