UITextView automatic scroll down

前端 未结 9 1705
遇见更好的自我
遇见更好的自我 2020-12-14 15:33

I have an UIView and I add a editable UITextView to it,

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 2         


        
9条回答
  •  孤城傲影
    2020-12-14 16:28

    When adding text to a text box programmatically, I found I also needed to add "[textView layoutIfNeeded]" to set things up before the new scroll position could be calculated correctly.

    for example:

    [newString appendFormat:@"%@\n",addText];
    textView.text = newString;
    [textView layoutIfNeeded];
    NSRange range = NSMakeRange(textView.text.length - 2, 1); //I ignore the final carriage return, to avoid a blank line at the bottom
    [textView scrollRangeToVisible:range]
    

    Without this addition the textbox would sometimes not scroll, or would scroll several lines every time a single line of data was added.

提交回复
热议问题