Edit
The simple solution is to move any frame calculations from viewDidLoad
to viewDidAppear:
I'm having a difficult time getting the following code to work properly
The code returns the first frame for a given NSRange in a UITextView.
It works if there are no line breaks, but I get some strange behavior when I add line breaks in the UITextView.
@implementation UITextView (TextFrame)
- (CGRect)frameOfTextRange:(NSRange)range {
UITextPosition *beginning = self.beginningOfDocument;
UITextPosition *start = [self positionFromPosition:beginning offset:range.location];
UITextPosition *end = [self positionFromPosition:start offset:range.length];
UITextRange *textRange = [self textRangeFromPosition:start toPosition:end];
CGRect rect = [self firstRectForRange:textRange];
return [self convertRect:rect fromView:self.textInputView];
}
@end
@implementation DetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.searchString) {
CGRect rect = [self.textView frameOfTextRange:[self.textView.text rangeOfString:self.searchString]];
...
}
}