【问题标题】:UITextView automatic scroll downUITextView 自动向下滚动
【发布时间】:2009-02-09 14:13:11
【问题描述】:

我有一个UIView,我向它添加了一个可编辑的UITextView

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 219, 47)];
[self addSubview:textView];
textView.font = [UIFont systemFontOfSize:14];

当我键入时,它不会自动关闭。有人有想法吗?

【问题讨论】:

    标签: cocoa-touch


    【解决方案1】:

    刚刚发现您必须使用长度> 0。即

    NSRange range = NSMakeRange(textView.text.length - 1, 1);
    [textView scrollRangeToVisible:range];
    

    无论如何都为我工作。

    【讨论】:

    • 谢谢,这很有帮助。您还需要检查 textView.text.length > 0.
    • 请注意,这将导致 textView 滚动到底部。把它放在 UITextView 的 textViewDidChange: 委托中不是一个好主意;如果您在文本视图的顶部进行编辑,它将滚动到底部,这不是用户所期望的。
    • 尝试了不同的解决方案,但这是一个很有魅力的解决方案
    • @ZS 我在使用此代码时遇到了这个问题。有什么办法可以解决这个问题?
    【解决方案2】:

    滚动到我尝试过的缓冲区的末尾

    [textView scrollRangeToVisible:NSMakeRange([textView.text length]-1, 1)];
    

    但是什么也没发生。 相反,这是可行的

    textView.selectedRange = NSMakeRange(textView.text.length - 1, 0);
    

    【讨论】:

      【解决方案3】:

      我认为你必须这样做

      [textView scrollRangeToVisible:[textView selectedRange]];
      

      textDidChange.

      【讨论】:

      • 嗯,奇怪。你能试试 [textView scrollRangeToVisible:NSMakeRange(textView.text.length, 0)];
      【解决方案4】:

      当以编程方式向文本框添加文本时,我发现我还需要添加“[textView layoutIfNeeded]”来进行设置,然后才能正确计算新的滚动位置。

      例如:

      [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]
      

      如果没有这个添加,文本框有时不会滚动,或者每次添加一行数据时都会滚动几行。

      【讨论】:

      • 这对我有用:[textView layoutIfNeeded]; [textView scrollRangeToVisible:NSMakeRange(0, 0)];
      【解决方案5】:

      斯威夫特 2.2。它对我有用

      func textViewDidChange(textView: UITextView) {
      
         let bottomOffset = CGPointMake(0, textView.contentSize.height - textView.bounds.size.height)
         textView.setContentOffset(bottomOffset, animated: true)
      }
      

      【讨论】:

      • 当 UITextView 大于其内容时它似乎可以工作,但是一旦内容变得大于屏幕上的大小,它就不再为我滚动了。
      【解决方案6】:

      我发现如果添加到 UITextView 的文本包含回车,那么 textview 将无法正确滚动。

      我添加了如下逻辑,以便在滚动到范围之前正确计算边界。

      BOOL autoscroll = NO;
      
      //I added a 10 px buffer here to allow for better detection as the textview is auto populated 
      if (textView.contentOffset.y >= (textView.contentSize.height - textView.frame.size.height - 10))
      {
          autoscroll = YES;
      }
      
      [textView setText:messageString];
      [textView layoutSubviews];
      
      if(autoscroll) {
          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)),     dispatch_get_main_queue(), ^{
              [textView scrollRangeToVisible:NSMakeRange(textView.text.length - 1, 1)];
          });
      }
      

      【讨论】:

        【解决方案7】:

        如果找到解决方案

        我将文本视图放入视图并将视图设置为 clipToBounds = YES 然后我把 textView 放到 70 的高度

        解决了问题。

        非常奇怪的解决方案,但它终于奏效了:)

        【讨论】:

        • 如果 70 低于您的 UITextView 的原始高度,那么它会被剪裁到该值,这可能是巧合,这正是您的案例“自动滚动”到最后一行所需要的。这有点太 hackish 了 :)
        【解决方案8】:

        这行得通:

        - (void)textViewDidChange:(UITextView *)textView {
            if (_moveToBottom) {
                int y = textView.contentSize.height - textView.frameHeight + self.textView.contentInset.bottom + self.textView.contentInset.top;
                if(y > 0) {
                    [textView setContentOffset:CGPointMake(0, y) animated:YES];
                }
                _moveToBottom = NO;
            }
            else {
                [textView scrollRangeToVisible:NSMakeRange([textView.text length]-1, 1)];
            }
        }
        
        - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
            if ([text isEqualToString:@"\n"] && range.location == [textView.text length]) {
                _moveToBottom = YES;
            }
            return YES;
        }
        

        【讨论】:

          【解决方案9】:

          使用contentOffset,它会自动向下滚动

          textView.contentOffset = CGPointMake(0, textView.contentSize.height - textView.frame.size.height);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-20
            • 2011-01-29
            相关资源
            最近更新 更多