首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在早于iOS 6.0的iOS版本中从UITextRange中获取矩形

如何在早于iOS 6.0的iOS版本中从UITextRange中获取矩形
EN

Stack Overflow用户
提问于 2012-12-26 16:39:09
回答 2查看 708关注 0票数 2

我想从UITextRange那里拿到所有的rects。您知道,如果UITextView换行,UITextRange将由多个CGRect表示。在iOS 6.0中,有一个名为"selectionRectsForRange:“的方法,所以你可以得到所有的CGRects。但在老版本中,只有一种方法“firstRectForRange”:我反复检查API文档,但什么也没有找到。

我的代码喜欢吹牛:

代码语言:javascript
复制
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];
EN

回答 2

Stack Overflow用户

发布于 2014-03-25 23:33:30

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

您必须重复调用firstRectForRange

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

票数 0
EN

Stack Overflow用户

发布于 2014-03-25 23:37:44

使用frameOfTextRange然后做一些类似这样的事情怎么样?但我不确定这是否适用于您的特定iOS版本。

代码语言:javascript
复制
- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView
{
    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]];
    CGRect rect = [textView firstRectForRange:textRange];
    return [textView convertRect:rect fromView:textView.textInputView];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14038012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档