UITextView在iOS16 beta系统上内容显示不全

2022-07-11  本文已影响0人  凤舞玖天

UITextView在iOS16 beta内容显示不全

UITextView如果通过

textView.editable=NO;

禁止编辑textView时,在iOS15上内容可以显示全,在iOS16上内容显示不全,该问题应该是系统的Bug。

解决方式:

添加以下代码:

textView.layoutManager.allowsNonContiguousLayout=NO;

官方注释中系统本身默认设置就为NO,但是不写就是显示不全。

设置为YES,也可以显示全。

// If YES, then the layout manager may perform glyph generation and layout for a given portion of the text, without having glyphs or layout for preceding portions.  The default is NO.  Turning this setting on will significantly alter which portions of the text will have glyph generation or layout performed when a given generation-causing method is invoked.  It also gives significant performance benefits, especially for large documents.

@property (NS_NONATOMIC_IOSONLY) BOOL allowsNonContiguousLayout API_AVAILABLE(macos(10.5), ios(7.0));

当我们在处理大量文本时,由于文本的布局过程包括字形生成和字形布局,所以对于连续布局来说,NSLayoutManager必须从头到尾计算文本所需要的所有字形以及所有布局信息,这就意味着,如果我们将一个TextView滚动到中间位置,我们需要计算从文本开始到此时显示的所有字形信息,但除了此时控件展示的内容,其余内容是我们看不见的,所以这种计算方式浪费了许多的性能。

为解决这个问题,苹果提供了不连续布局,该布局方式会将大量文本分割为多个布局,当对控件进行滚动时,系统不需要计算所有的字形及布局,只需要计算所需显示的那部分布局的字形,这就大大提高了性能。

那么如何开启不连续布局呢?

对于NSTextView来说,设置allowsNonContiguousLayout即可开启不连续布局。

对于UITextView来说,allowsNonContiguousLayout默认是开启的,需要一个前提条件:由于UITextView是UIScrollView的子类,所以需要打开滚动属性才能激活不连续布局。

上一篇下一篇

猜你喜欢

热点阅读