UITextView beginningOfDocument returns nil

Viewed 1762

I have to get the position on the screen of a string contained in a UITextView. To do that, I tried to get the corresponding UITextPosition(s). The problem is that the UITextView method beginningOfDocument returns nil (same for endOfDocuement). Could someone explain me why? And which solution I could use.

My code looks like this:

- (UITextRange *)getRangeOfString:(NSString *)string inTextView:(UITextView *)tv
{
    UITextPosition *beginning = [tv positionFromPosition:tv.beginningOfDocument offset:[tv.text rangeOfString:string].location];
    UITextPosition *end = [tv positionFromPosition:beginning offset:[tv.text rangeOfString:string].length];

    return ([tv textRangeFromPosition:beginning toPosition:end]);
}
2 Answers
Related