Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find position or get rect of any word in textview and place buttons over that?

I am working on a story app.Where We need to provide quizes. Now I am having a story and there are some blanks or hidden words over that story. Whenever I click over that hidden word,I will get the 4 options to answer that. I tried by placing button over the words,But that would be only when I am using some static position. I just want to know how can I get the frame of that word,which I need to hide ,so that I can place some button over that and can hide this.

You can see the image below..enter image description here All answers would be appreciated

Solution:- This will work after iOS 5 only not below that !

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



}

Thanks

See one more image for errors: enter image description here

like image 813
Sabby Avatar asked Apr 25 '12 10:04

Sabby


1 Answers

UITextView adopts the UITextInput protocol. That protocol has the method -firstRectForRange: which will tell you the rectangle covering a range of characters. If the range of characters spans multiple lines, you have to call it repeatedly. The first time it will give you the first rect and tell you what range that covers. Then, you adjust the range you're asking about to be the remaining characters you're interested in and repeat.

like image 146
Ken Thomases Avatar answered Sep 28 '22 05:09

Ken Thomases