iphone - ios - 如何找到 UITextView 中文本的可见范围?

标签 iphone ios uiscrollview uitextview

如何找到可滚动、不可编辑的 UITextView 中可见的文本?

例如我可能需要显示下一段,然后我想找到当前可见的文本范围并用它来计算合适的范围并使用scrollRangeToVisible:滚动 TextView

最佳答案

我在这里找到了另一个解决方案。 在我看来,这是解决这个问题的更好方法。 https://stackoverflow.com/a/9283311/889892

由于 UITextView 是 UIScrollView 的子类,其 bounds 属性反射(reflect)了其坐标系的可见部分。所以这样的事情应该有效:

-(NSRange)visibleRangeOfTextView:(UITextView *)textView {
    CGRect bounds = textView.bounds;
    UITextPosition *start = [textView characterRangeAtPoint:bounds.origin].start;
    UITextPosition *end = [textView characterRangeAtPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))].end;
    return NSMakeRange([textView offsetFromPosition:textView.beginningOfDocument toPosition:start],
        [textView offsetFromPosition:start toPosition:end]);
}

这假定了从上到下、从左到右的文本布局。如果你想让它适用于其他布局方向,你将不得不更加努力。 :)

关于iphone - ios - 如何找到 UITextView 中文本的可见范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6067803/

相关文章:

c - 在 iPhone 上进行大量的 malloc 和 free 操作是否很慢?

iphone - iOS 可以批量发送短信吗?

ios - 使用 UINavigationController 和 UINavigationBar 时图层的 y 位置动画错误

ios - 在 UIScrollView 的 Paging 上重用 3 个 View

ios - 以编程方式滚动 UIScrollView 但不仅仅在委托(delegate)方法中处理此操作?

iphone - IBAction 函数的命名约定

iphone - 核心数据的最大存储容量是多少?

ios - Objective-C中基于触摸的图像处理

objective-c - 如何在iOS中将图像裁剪成多边形?

UIScrollView 的 iPhone 内存警告问题