在iOS7或TextKit中使用什么而不是scrollRangeToVisible [英] what to use instead of scrollRangeToVisible in iOS7 or TextKit

查看:102
本文介绍了在iOS7或TextKit中使用什么而不是scrollRangeToVisible的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以前版本的iOS中,我的 UITextView 将使用

In previous versions of iOS, my UITextView will scroll to the bottom using

[displayText scrollRangeToVisible:NSMakeRange(0,[displayText.text length])];

CGFloat topCorrect = displayText.contentSize.height -[displayText bounds].size.height;
topCorrect = (topCorrect<0.0?0.0:topCorrect);
displayText.contentOffset = (CGPoint){.x=0, .y=topCorrect};

但前者现在会产生奇怪的效果,从长篇文本的顶部开始,每次我将文本追加到视图时,动画滚动到底部。当我添加文字时,有没有办法弹出文本的底部?

But the former will now have the weird effect of starting at the top of a long length of text and animating the scroll to the bottom each time I append text to the view. Is there a way to pop down to the bottom of the text when I add text?

推荐答案

对于未来的旅行者,建立的@ mikeho的帖子,我发现了一些对我有用的东西,但有点简单。

For future travelers, building off of @mikeho's post, I found something that worked wonders for me, but is a bit simpler.

1)确保你的 UITextView contentInset 已正确设置&在执行此操作之前,你的textView已经是 firstResponder()

2)在我的插件准备就绪后,光标处于活动状态时,我调用以下函数:

1) Be sure your UITextView's contentInsets are properly set & your textView is already firstResponder() before doing this.
2) After my the insets are ready to go, and the cursor is active, I call the following function:

private func scrollToCursorPosition() {
    let caret = textView.caretRectForPosition(textView.selectedTextRange!.start)
    let keyboardTopBorder = textView.bounds.size.height - keyboardHeight!

   // Remember, the y-scale starts in the upper-left hand corner at "0", then gets
   // larger as you go down the screen from top-to-bottom. Therefore, the caret.origin.y
   // being larger than keyboardTopBorder indicates that the caret sits below the
   // keyboardTopBorder, and the textView needs to scroll to the position.
   if caret.origin.y > keyboardTopBorder {
        textView.scrollRectToVisible(caret, animated: true)
    }
 }

这篇关于在iOS7或TextKit中使用什么而不是scrollRangeToVisible的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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