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

前端 未结 3 1550
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 18:10

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 hi

相关标签:
3条回答
  • 2020-12-28 18:58

    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.

    0 讨论(0)
  • 2020-12-28 19:03

    Following on from Ken Thomases' answer, from iOS 6 and onwards the UITextInput protocol includes the following method:

    - (NSArray *)selectionRectsForRange:(UITextRange *)range

    (See the reference doc).

    The result is an array of UITextSelectionRects, which have a bunch of information about selection ranges in text... Depending on how you want to use the rects of text-chunks, the above method might be useful.

    (UITextSelectionRect reference).

    Note: from my usage with UITextView, the array of UITextSelectionRects returned contains, what might be interpreted as, two additional selection rects. These are the last two selection rects in the array and I assume they correspond to the start and end positions of the selection. These selection rects each have a width of 0 and the containsStart / containsEnd properties are set accordingly.

    0 讨论(0)
  • 2020-12-28 19:14
    -(void)textViewDidChangeSelection:(UITextView *)textView
     {
      NSRange range = textView.selectedRange;
     if(range.location<textView.text.length)
      {
        NSString * firstHalfString = [txtView.text substringToIndex:range.location];
    
        NSString *temp = @"s";
        CGSize s1 = [temp sizeWithFont:[UIFont systemFontOfSize:15] 
                              constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)  // - 40 For cell padding
                                  lineBreakMode:UILineBreakModeWordWrap]; // enter you textview font size
    
        CGSize s = [firstHalfString sizeWithFont:[UIFont systemFontOfSize:15] 
                               constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)  // - 40 For cell padding
                                   lineBreakMode:UILineBreakModeWordWrap]; // enter you textview font size
    
    
        //Here is the frame of your word in text view.
        NSLog(@"xcoordinate=%f, ycoordinate =%f, width=%f,height=%f",s.width,s.height,s1.width,s1.height);
    
         CGRect rectforword = CGRectMake(s.width, s.height, s1.width, s1.height);
        // rectforword is rect of yourword
    
    
        }
        else
        {
            // Do what ever you want to do
    
         }   
    }
    

    >>>Edited...

    This method is call when you select or tap on the textview text but you can use this method in any of UITextView Delegate Method

    Hope, this will help you...enjoy

    0 讨论(0)
提交回复
热议问题