【问题标题】:UITextView contentOffset not working on iOS 7UITextView contentOffset 在 iOS 7 上不起作用
【发布时间】:2013-10-19 23:05:17
【问题描述】:

我的应用中有一个 UITextView。每次添加新字符串时,我都需要动态更改它的内容偏移量。下面的代码在 iOS 6 和更早的版本上运行良好,但在 iOS 7 上不行。

TextViewText.text = [TextViewText.text stringByReplacingCharactersInRange:RecentWordRange withString:string];
   newStringLen = string.length;
   [TextViewText setSelectedRange: NSMakeRange(RecentWordRange.location+string.length, 0)];
   [TextViewText setContentOffset: CGPointMake(0,0) animated:NO];
   [TextViewText setContentOffset:contentOffset animated:YES];

在 IOS7 中 setContentOffset 有何变化?

【问题讨论】:

标签: ios7 uitextview


【解决方案1】:

最近我在 iOS 8 中解决了一个类似的问题。我在文本视图中替换了文本,它跳来跳去。使用 UITextRange 相关方法有帮助。

UITextRange *RecentWordRange = TextViewText.selectedTextRange;
[TextViewText replaceRange:RecentWordRange withText:string];

【讨论】:

    【解决方案2】:

    使用 UITextView 的 textContainerInset 表示 insets,使用 (UIEdgeInsets)contentInset 表示 (CGPoint)contentOffset

    不知道为什么有人认为这是处理 UITextView 偏移的最佳方法,但这对我有用。

    还要注意有一个奇怪的错误,它不接受 textView 上的插入,除非有左右偏移。所以请确保左/右 UIEdgeInsets 大于 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多