美文网首页
UITextView实现部分文字点击跳转效果

UITextView实现部分文字点击跳转效果

作者: 西贝人立口 | 来源:发表于2024-07-07 15:40 被阅读0次
在开发中经常遇到登录界面 【请同意《隐私协议》和《隐私内容》】 如果使用button、label进行拼接十分费劲。可以考虑使用UITextView来实现部分文字跳转的效果。
如图: image.png

步骤如下:
一、实现UITextView

-(UITextView *)agreementTF {
    if (!_agreementTF) {
        _agreementTF = [[UITextView alloc] init];
        _agreementTF.editable = NO;
        _agreementTF.scrollEnabled = NO;
        _agreementTF.selectable = NO;
        _agreementTF.backgroundColor = [UIColor clearColor];
        _agreementTF.font = kMediumFontOfSize(12);
        NSString *fristStr  = @"阅读并同意";
        NSString *secondStr = [NSString stringWithFormat:@"《%@会员服务协议》",[XZScreenUnit manager].app_name];
        NSString *allStr    = [NSString stringWithFormat:@"%@%@",fristStr,secondStr];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:allStr];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#999999"] range:[[attributedString string] rangeOfString:fristStr]];
        [attributedString addAttribute:NSLinkAttributeName value:@"firstmanager" range:[[attributedString string] rangeOfString:secondStr]];
        _agreementTF.attributedText = attributedString;
        //设置点击部分的文字颜色
        _agreementTF.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#03CA82"]};
        //为textview添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addGestureRecognizer:)];
        [_agreementTF addGestureRecognizer:tap];
    }
    return _agreementTF;
}

注意:
1.把UITextView的编辑、滚动、选中都置为NO。
2.为textview添加手势,通过关键字区分点击事件。上面关键字为firstmanager,并且确保下面一致。
3.上面代码中app_name为获取app的名字拼接的。
二、实现手势方法

#pragma mark -- textVeiw 手势及自定义方法实现
- (void)addGestureRecognizer:(UITapGestureRecognizer*)gestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        CGPoint topLocation = [gestureRecognizer locationInView:self.agreementTF];
        UITextPosition *textPosition = [self.agreementTF closestPositionToPoint:topLocation];
        
        NSDictionary *attributes = [self.agreementTF textStylingAtPosition:textPosition inDirection:UITextStorageDirectionBackward];
        NSURL *url = attributes[NSLinkAttributeName];
     
        if(url) {
            NSString *secondStr = [NSString stringWithFormat:@"《%@会员服务协议》",[XZScreenUnit manager].app_name];
            NSRange range = [self.agreementTF.text rangeOfString:secondStr];
            [self textViews:self.agreementTF houldInteractWithURL:url inRange:range];
            
        }
    }
}
//实现手势里面的方法
- (void)textViews:(UITextView *)textViews houldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange{
    if ([(NSString *)URL isEqualToString:@"firstmanager"]) {
        //此处进行网页界面跳转
    }
}

注意:firstmanager关键字要和上面保持一致。

相关文章

网友评论

      本文标题:UITextView实现部分文字点击跳转效果

      本文链接:https://www.haomeiwen.com/subject/qbzncjtx.html