将UITextView滚动到底部 [英] Scroll UITextView To Bottom

查看:480
本文介绍了将UITextView滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个具有 UITextView 的应用程序和一个按钮。

I am making a an app that has a UITextView and a button.

当我点击按钮时,一些文本将添加到 UITextView 中。

When I click the button some text will add in the UITextView.

但是当点击按钮时,我不想向下滚动到文本字段的底部,以便用户可以看到最后添加的文本。

But when clicking the button, I wan't to scroll down to the bottom of the text field so the user can see the last text added.

如何让 UITextView 向下滚动到底部?

How to make the UITextView to scroll down to the bottom?

我试过了:

int numLines = LogTextView.contentSize.height / LogTextView.font.lineHeight+1;
NSLog(@"%d",numLines);

NSUInteger length = self.LogTextView.text.length;
self.LogTextView.selectedRange = NSMakeRange(0, length);

但它不起作用...

我也尝试过:

self.LogTextView.contentSize=CGSizeMake(length,0);


推荐答案

如果您正在谈论的话,可以使用以下代码UITextView:

You can use the following code if you are talking about UITextView:

-(void)scrollTextViewToBottom:(UITextView *)textView {
     if(textView.text.length > 0 ) {
        NSRange bottom = NSMakeRange(textView.text.length -1, 1);
        [textView scrollRangeToVisible:bottom];
     }

}

SWIFT 4:

func scrollTextViewToBottom(textView: UITextView) {
    if textView.text.count > 0 {
        let location = textView.text.count - 1
        let bottom = NSMakeRange(location, 1)
        textView.scrollRangeToVisible(bottom)
    }
}

这篇关于将UITextView滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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