iOS 富文本+点击事件

UITextView *agreementTextView = [[UITextView alloc] init];
    [self.agreementView addSubview:agreementTextView];
    [agreementTextView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.agreementBtn];
    [agreementTextView autoAlignAxisToSuperviewMarginAxis:ALAxisHorizontal];
    agreementTextView.font = FONT_13;
    agreementTextView.text = agreementStr;
    agreementTextView.backgroundColor = [UIColor clearColor];
    agreementTextView.delegate=self;
    //必须禁止输入,否则点击将弹出输入键
    agreementTextView.editable=NO;
    agreementTextView.scrollEnabled=NO;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing= 1;
    NSDictionary*attributes = @{NSFontAttributeName:FONT_13,
                                NSParagraphStyleAttributeName:paragraphStyle};
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:agreementTextView.text attributes:attributes];
    [attributedString addAttribute:NSLinkAttributeName value:@"yonghuxieyi://" range:NSMakeRange(6,6)];
    [attributedString addAttribute:NSLinkAttributeName value:@"yisizhengce://" range:NSMakeRange(13,6)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:kColorWithRGBA(255, 255, 255, 0.5) range:NSMakeRange(0,agreementTextView.text.length)];
    agreementTextView.attributedText= attributedString;
    //设置被点击字体颜色
    agreementTextView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};

#pragma mark 富文本点击事件
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    if ([[URL scheme] isEqualToString:@"yonghuxieyi"]) {
        NSLog(@"富文本点击 用户协议");
        WebViewVC *vc = [[WebViewVC alloc] init];
        vc.url = [NSString stringWithFormat:@"%@%@", kApiBaseUrl, kAgreement];
        vc.title = kString(@"用户协议");
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([[URL scheme] isEqualToString:@"yisizhengce"]) {
        NSLog(@"富文本点击 隐私政策");
        WebViewVC *vc = [[WebViewVC alloc] init];
        vc.url = [NSString stringWithFormat:@"%@%@", kApiBaseUrl, kAgreement];
        vc.title = kString(@"隐私政策(暂无)");
        [self.navigationController pushViewController:vc animated:YES];
    }
    return YES;
}

效果图

截屏2020-09-03下午1.44.27.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容