ios - 如何从iOS 6.0之前的iOS版本中的UITextRange获取整流器

标签 ios range cgrect uitextrange

我想从UITextRange中获取所有区域。您知道,如果UITextView换行,则UITextRange将由1个以上CGRect表示。在iOS 6.0中,有一个名为“selectionRectsForRange:”的方法,因此您可以获得所有CGRects。但是在旧版本中,只有方法“firstRectForRange:”
我一遍又一遍地检查了API文档,但是什么也没找到。

我的代码喜欢打击:

UITextPosition *beginning = textView.beginningOfDocument;
UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
UITextPosition *end = [textView positionFromPosition:start offset:range.length];
UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];

//(in iOS 6.0)      
NSArray *array = [textView selectionRectsForRange:textRange];
for (int i=0; i<[array count]; i++) {
    NSLog(@"rect array = %@", NSStringFromCGRect([[array objectAtIndex:i] rect]));
}
// (in earlier version)
CGRect rect = [textView firstRectForRange:textRange];

最佳答案

根据此答案中的信息:
How to find position or get rect of any word in textview and place buttons over that?

您必须反复调用firstRectForRange

当您得到第一个矩形时,您将能够检查其范围。使用这个值和您最初提供的范围,您可以使用调整后的范围再次调用firstRectForRange(如果需要)...

关于ios - 如何从iOS 6.0之前的iOS版本中的UITextRange获取整流器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14038012/

相关文章:

ios - Swift segue - 不鼓励在分离的 View Controller 上呈现 View Controller

ios - 保留 UISearchBar 的边距

ios - 无法在ios中的webview中保存图像

python - 是否有一个 numpy 函数可以让你指定开始、步骤和数字?

swift - 使用 UIPanGestureRecognizer 拖动 CGRect

objective-c - 使用 substringFromIndex : with NSData

python - 使用 Python 创建一个范围内的值依赖于另一列的列

Excel:如何在数据透视表中创建自定义年龄范围组?

ios - 如何获取尺寸并加载PDF页面?

ios - UILabel Padding 基于内容