【问题标题】:UITextView in UITableViewCell how to scroll table view to keep cursor visible while typingUITableViewCell中的UITextView如何滚动表格视图以在键入时保持光标可见
【发布时间】:2016-04-11 01:10:37
【问题描述】:

我有一个包含 UITextView 的自定义 UITableViewCell。细胞相当大。如何自动滚动表格以确保当前选择点始终可见?

注意 UITextView 是单元格的大小,因此它不会自动滚动。

我可以在 selectedTextRange 上调用 firstRectForRange,但如果插入点位于末尾,则此方法不起作用(CGRect 为 inf,inf,0,0)。如果我有矩形,我可以计算表中的偏移量并调整它的滚动内容偏移量。有什么好的方法可以做到这一点,还是弄乱 contentOffset 是唯一的方法?

【问题讨论】:

    标签: ios swift uitableview uitextview


    【解决方案1】:

    在 textviewdidchange 中使用它

    if let confirmedTextViewCursorPosition = textView.selectedTextRange?.end {
    
                let caretPosition = textView.caretRect(for: confirmedTextViewCursorPosition)
                var textViewActualPosition = tableView.convert(caretPosition, from: textView.superview?.superview)
                textViewActualPosition.origin.y += 20.0 // give the actual padding of textview inside the cell
                tableView.scrollRectToVisible(textViewActualPosition, animated: false)
    
            }
    

    【讨论】:

      【解决方案2】:

      这有什么好运气吗?我遇到了完全相同的情况。我目前正在做的是使用

      获得插入符号位置的矩形
      [textView caretRectForPosition:textView.selectedTextRange.end]
      

      然后尝试从

      convertRect: fromView:
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 2011-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多