简体   繁体   中英

Scroll UITextView To Bottom

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

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.

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

I tried:

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

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

but it will not work...

I also tried:

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

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

Try this if you have problem on iOS 7 or above. See 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];
}

With Swift 3

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

Swift 5

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

// Usage
textView.simple_scrollToBottom()

You have to implement a delegate method. The code below checks whether a newline has been entered and, if so, scrolls to the bottom of the 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;
}

Make a range, specifying encoding, to the last character, then scroll to that range Something other than utf8 might be appropriate depending on your content

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

This works for me! :D

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

As a generic approach for scrolling to bottom, it can be done on a UIScrollView.

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

This will work on all descendants of UIScrollView like UITextView, UITableView etc..

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

This solution does a couple of notable things slightly different:

  • Utilizes the String.Index interface (likely more performant than eg .count )
  • Uses a PartialRangeUpTo which avoids an explicit range start position, reducing the code to a clean one-liner

The Swift version of @Hong Duan answer

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM