Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView beginningOfDocument returns nil

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]);
}
like image 894
Aeradriel Avatar asked Dec 06 '13 14:12

Aeradriel


1 Answers

This is about a month late, but make sure you have the "selectable" property enabled in Interface Builder.

Credit: http://jon-nolen.blogspot.com/2013/10/uitextview-returns-nil-for-uitextinput.html

like image 50
Gazzini Avatar answered Sep 19 '22 18:09

Gazzini