2012-12-26 91 views
1

我想要得到UITextRange所有rects。你知道,如果UITextView换行,UITextRange将由多于1个CGRect表示。在iOS 6.0中,有一个方法是“selectionRectsForRange:”,所以你可以得到所有的CGRects。但在旧版本中,只有一个方法“firstRectForRange:” 我一遍又一遍检查的API文件,但我觉得没什么。如何从UITextRange的rects在早期的iOS版本的iOS比6.0

我的代码喜欢的打击:

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]; 

回答

0

如何使用frameOfTextRange,然后做这样的事情。但是我不确定这是否适用于您的特定iOS版本。

- (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]; 
} 
相关问题