新行中 ScrollView 插入符号错位的 iOS TextView

标签 ios swift uiscrollview autolayout uitextview

我有以下布局 well know approach在 ScrollView 中使用 TextView。


enter image description here


它总体上工作得很好,但有一个小问题让我很恼火。


enter image description here

看看插入符号。插入新行时,自动版式不会更新。它只会在添加新字符时更新约束,突然该行再次正确显示。 对于上下文,这在仅使用不带 scrollView 的 TextView 时效果非常好,所以我想知道我是否需要手动执行更多操作来模拟该行为。

现在我在 textViewDidChange 委托(delegate)上触发了一个 view.layoutIfNeeded() 来解决这个问题,但我觉得这太过分了。

提前致谢。

最佳答案

好问题!

在您的 textViewDidChange 方法中使用它:

- (void)textViewScrollToCarretIfNeeded:(UITextView *)textView {
    CGRect caretRectInsideTextView = [textView caretRectForPosition:textView.selectedTextRange.start];
    CGRect caretRectInsideScrollView = [self.scrollView convertRect:caretRectInsideTextView fromView:textView];
    [self.scrollView scrollRectToVisible:caretRectInsideScrollView animated:YES];
}

您可以使用在线服务将其转换为 swift。

关于新行中 ScrollView 插入符号错位的 iOS TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45032047/

相关文章:

ios - 在 ios 中自动滚动的无限 Textview

ios - 具有多个 UIView 的 UIScrollView

ios - UIScrollView 不保持其滚动位置

android - 在 Windows 中获取 React Native IOS .ipa 文件

ios - 如何避免在回调/等中访问已释放的对象?

objective-c - 我的协议(protocol)方法的选择器没有已知的实例方法

ios - 将数据从 Container View 不带 Segue 传递到 MainVC

string - 比较 String.Index 值

ios - 在带有 Cocoapods 的 Swift 中使用 Evernote iOS API 在 Xcode 中找不到文件错误

arrays - 根据输入 swift 组装一个变量