如何有效地找到UITextView中可见单词的CGRect? [英] How to efficiently find CGRects for visible words in UITextView?

查看:66
本文介绍了如何有效地找到UITextView中可见单词的CGRect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在UITextView中标记所有可见的拼写错误的单词.

My goal is to mark all visible misspelled words in an UITextView.

低效率的算法是使用拼写检查器查找文本中所有拼错单词的范围,然后使用positionFromPosition:inDirection:offset等将其转换为UITextRange对象,然后使用UITextInput方法firstRectFromRange获取图形区域.

The inefficient algorithm is to use the spell checker to find all ranges of misspelled words in the text, convert them to UITextRange objects using positionFromPosition:inDirection:offset etc, then get the graphics rects using the UITextInput method firstRectFromRange.

因此所有文本->拼写错误的单词-> NSRange集合-> UITextRange集合-> CGRect集合->评估可见性,绘制可见的

Thus all the text -> misspelled words-> NSRange collection -> UITextRange collection -> CGRect collection -> evaluate for visibility, draw visible ones

问题在于,这需要检查所有文本,并将所有拼写错误的单词转换为图形矩形.

The problem is that this requires that all the text is checked, and all misspelled words are converted to graphics rects.

因此,我想办法是以某种方式找出UITextView中当前可见的基础.text的哪些部分.

Thus, I imagine the way to go is to somehow find out what parts of the underlying .text in the UITextView that is visible at the moment.

因此可见的文本范围->拼写错误的单词-> NSRange集合-> UITextRange集合-> CGRect集合->计算可见性,绘制可见的

Thus for range of text visible -> misspelled words-> NSRange collection -> UITextRange collection -> CGRect collection -> evaluate for visibility, draw visible ones

ios-如何在UITextView中找到可见的文本范围?可以作为一种方法来绑定要检查的文本的哪些部分,但仍然需要对所有文本进行测量,我认为这可能会非常昂贵

The code in ios - how to find what is the visible range of text in UITextView? might work as a way to bound what parts of the text to check, but still requires that all text is measured, which I imagine could be quite costly.

有什么建议吗?

推荐答案

由于UITextViewUIScrollView的子类,因此它的bounds属性反映了其坐标系的可见部分.所以这样的事情应该起作用:

Since UITextView is a subclass of UIScrollView, its bounds property reflects the visible part of its coordinate system. So something like this should work:

- (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]);
}

这假定从上到下,从左到右的文本布局.如果要使其适用于其他布局方向,则必须更加努力. :)

This assumes a top-to-bottom, left-to-right text layout. If you want to make it work for other layout directions, you will have to work harder. :)

这篇关于如何有效地找到UITextView中可见单词的CGRect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆