将UITextView滚动到底部

时间:2013-05-22 18:00:39

标签: ios uitextview

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

点击按钮后,UITextView会添加一些文字。

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

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

我试过了:

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);

10 个答案:

答案 0 :(得分:60)

如果您正在谈论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)
    }
}

答案 1 :(得分:13)

如果您在iOS 7或更高版本上遇到问题,请尝试此操作。请参阅this SO answer

- (void)scrollTextViewToBottom:(UITextView *)textView {
    NSRange range = NSMakeRange(textView.text.length, 0);
    [textView scrollRangeToVisible:range];
    // an iOS bug, see https://stackoverflow.com/a/20989956/971070
    [textView setScrollEnabled:NO];
    [textView setScrollEnabled:YES];
}

答案 2 :(得分:7)

使用Swift 3

let bottom = self.textView.contentSize.height - self.textView.bounds.size.height
self.textView.setContentOffset(CGPoint(x: 0, y: bottom), animated: true)

答案 3 :(得分:5)

您必须实现委托方法。下面的代码检查是否输入了换行符,如果是,则滚动到textView的底部:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    if ([text isEqualToString:@"\n"]) {
        textView.contentOffset = CGPointMake(0.0, textView.contentSize.height);
    }
    return YES;
}

答案 4 :(得分:3)

这对我有用! :d

CGPoint bottomOffset = CGPointMake(0, self.textView.contentSize.height - self.textView.bounds.size.height);
[self.description1 setContentOffset:bottomOffset animated:YES];

答案 5 :(得分:3)

Swift 4

extension UITextView {
    func simple_scrollToBottom() {
        let textCount: Int = text.count
        guard textCount >= 1 else { return }
        scrollRangeToVisible(NSMakeRange(textCount - 1, 1))
    }
}

// Usage
textView.simple_scrollToBottom()

我没有使用NSRange找不到解决方案。

答案 6 :(得分:2)

为最后一个字符创建一个指定编码的范围,然后滚动到该范围 除了utf8之外的其他内容可能是合适的,具体取决于您的内容

let range = NSMakeRange(self.OutputTextView.text.lengthOfBytes(using: String.Encoding.utf8), 0);
self.OutputTextView.scrollRangeToVisible(range);

答案 7 :(得分:2)

作为滚动到底部的通用方法,可以在UIScrollView上完成。

extension UIScrollView {
    func scrollToBottom() {
        let contentHeight = contentSize.height - frame.size.height
        let contentoffsetY = contentHeight > 0 ? contentHeight : 0
        setContentOffset(CGPoint(x: 0, y: contentoffsetY), animated: true)
    }
}

这将适用于UIScrollView的所有后代,如UITextView,UITableView等。

答案 8 :(得分:1)

textView.scrollRangeToVisible(NSRange(..<textView.text.endIndex, in: textView.text))

此解决方案做了一些值得注意的事情,略有不同:

  • 使用String.Index界面(性能可能比.count高)
  • 使用PartialRangeUpTo来避免出现明确的范围起始位置,从而将代码简化为简洁的一行代码

答案 9 :(得分:0)

@Hong Duan answer

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

        // an iOS bug, see https://stackoverflow.com/a/20989956/971070
        textView.isScrollEnabled = false
        textView.isScrollEnabled = true
    }
}
相关问题