6

[iOS] UITextView 追加文字自动跳到顶部解决方法

 2 years ago
source link: https://www.isaced.com/post-266.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

[iOS] UITextView 追加文字自动跳到顶部解决方法

最近在做一个项目里遇到类似于网游公共对话窗口的文本框,由于需求比较简单,就用了 UITextView 实现,再配合 NSAttributedString 可以很轻松地实现颜色、字体等自定义;但是当我重新赋值设置 TextView 的 textattributedText 属性时,会自动滑动回顶部,但我需要 TextView 超出屏幕始终定位在底部,于是我用 scrollRangeToVisible: 方法在 Append 后手动滑动到底部,可是还是会先由系统滑动到顶部,再滑倒底部,这个动画看起来很龊,于是开始了漫长的求解寻找之中…

找寻几天没结果,今日再次 Google 而翻到一篇日语的博客,遇到的情况大致类似: > iOS8,iOS7にてUITextViewで最後の行をTapするとTopにScrollする現象(ぶっ飛び怪奇現象)の回避方法

textView.layoutManager.allowsNonContiguousLayout = NO;

这句代码设置了 UITextView 中的 layoutManager(NSLayoutManager) 的是否非连续布局属性,默认是 YES,设置为 NO 后 UITextView 就不会再自己重置滑动了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK