Get word from long tap in a word of UITextView

后端 未结 2 1477
故里飘歌
故里飘歌 2020-12-29 16:03

Now I already detect long tap in UITextView

    - (void)viewDidLoad
    {
         [super viewDidLoad];
         UILongPressGestureRecognizer *LongPressgest         


        
2条回答
  •  旧时难觅i
    2020-12-29 16:57

    This function will return the word at a given position in an UITextView.

    +(NSString*)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv
    {
        //eliminate scroll offset
        pos.y += _tv.contentOffset.y;
    
        //get location in text from textposition at point
        UITextPosition *tapPos = [_tv closestPositionToPoint:pos];
    
        //fetch the word at this position (or nil, if not available)
        UITextRange * wr = [_tv.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
    
        return [_tv textInRange:wr];
    }
    

提交回复
热议问题