iOS uitextview 字符检测 link检索 UITapGestureRecognizer 点击和 didSelectRowAtIndexPath 冲突

该代码段展示了如何在Objective-C中检测UITextView中的链接点击。当用户点击文本时,它会获取点击位置的字符索引,判断是否包含链接,并根据链接类型执行相应操作,如跳转页面或显示评论窗口。

//检测uitextview 文本 获取 link

-(void)uiTextViewClick:(UITapGestureRecognizer*)tap{

    if ([tap isKindOfClass:[UITapGestureRecognizer class]])

    {

        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:tap.view.tag+2];

        EMGoodsCommentCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];

        NSLayoutManager *layoutManager = cell.lblContent.layoutManager;

        CGPoint location = [tap locationInView:cell.lblContent];

        location.x -= cell.lblContent.textContainerInset.left;

        location.y -= cell.lblContent.textContainerInset.top;

        //点击的字符索引

        NSUInteger characterIndex =  [layoutManager glyphIndexForPoint:location inTextContainer:cell.lblContent.textContainer];

        CGRect glyphRect = [layoutManager boundingRectForGlyphRange:NSMakeRange(characterIndex,1) inTextContainer:cell.lblContent.textContainer];

        //EM示例 字符检测 判断是否点击的字符

        if(CGRectContainsPoint(glyphRect,location))

        {

            //检测是否包含超级链接

            CGPoint tapLocation = [tap locationInView:cell.lblContent];

            UITextPosition* textPosit = [cell.lblContent closestPositionToPoint:tapLocation];

            NSDictionary *attributes = [cell.lblContent textStylingAtPosition:textPosit inDirection:UITextStorageDirectionForward];

            NSString *link = attributes[NSLinkAttributeName];

            NSURL *url = [[NSURL alloc] initWithString:[[NSString stringWithFormat:@"%@",link] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet  URLQueryAllowedCharacterSet]]];

            

            //点击的字符但是未点击link

            if(link==nil)

            {

                //弹出评论回复窗口

                [self commentWindow:indexPath];

            }

            

            if (characterIndex < cell.lblContent.textStorage.length) {

                NSRange range;

                //获取超级链接字符的索引

                id value = [cell.lblContent.attributedText attribute:[url host] atIndex:characterIndex effectiveRange:&range];

                //判断链接字符位置

                if(characterIndex>=range.location&&characterIndex<=(range.location+range.length))

                {

                    if(link)

                    {

                        if ([[url scheme] isEqualToString:@"callUser"]) {

                            EMMyHomeController* page = [[EMMyHomeController alloc] init];

                            id params = @{@"userId":[url query]};

                            [[PageManager shareInstance] gotoPage:page withParams:params];

                        }else if ([[url scheme] isEqualToString:@"talk"])

                        {

                            NSLog(@"跳转话题界面 未实现");

                        }

                    }

                }

            }

        }

        else

        {

            NSLog(@"点击空白处");

            //弹出评论回复窗口

            [self commentWindow:indexPath];

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值