点击富文本部分文字跳转功能

简介: 点击富文本部分文字跳转功能

点击富文本部分文字跳转功能。

通常用在统一用户协议,隐私协议,儿童协议,移动认证服务条款。

注意实现:由于采用UITextView实现,一行显示没有问题,但是当需要多行显示时,由于他有行间距和头部空白取,不会像UILabel定格显示,需要适当增加高度来显示多行,高度设置小了只显示一行,不会换行。需要适当向上微移来进行对齐其它UI元素。

网上那种只在UILabel添加手势事件的方式,显示无法控制点击指定部分文字跳转,多UILabel的方式也不能处理换行的情况。一般的协议大都有多个,那种方案显然无法实现。我这种方案才是最终实现方案。

具体代码:

1.设置协议:

@interface QYLoginPanel ()<UITextViewDelegate>

2.增加UITextView对象和添加富文本,这个是核心:

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc]init];
        _textView.backgroundColor = UIColor.clearColor;
        _textView.textColor = RGBA(255, 255, 255, 0.6);
        _textView.font = [UIFont systemFontOfSize:12];
        
        NSString *str = @"注册即表示同意《用户隐私》《隐私协议》以及《移动认证服务条款》";
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:str];
        [att addAttribute:NSLinkAttributeName value:@"privacyPolicy://" range:NSMakeRange(14, 4)];
        [att addAttribute:NSLinkAttributeName value:@"userProtocol://" range:NSMakeRange(8, 5)];
        [att addAttribute:NSLinkAttributeName value:@"mobileProtocol://" range:NSMakeRange(22, 8)];
        [att addAttribute:NSForegroundColorAttributeName value:RGBA(255, 255, 255, 0.6) range:NSMakeRange(0, str.length)];
//        [att addAttribute:NSForegroundColorAttributeName value:RGBA(255, 255, 255, 1.0) range:NSMakeRange(8, 4)];
//        [att addAttribute:NSForegroundColorAttributeName value:RGBA(255, 255, 255, 1.0) range:NSMakeRange(14, 4)];
//        [att addAttribute:NSForegroundColorAttributeName value:RGBA(255, 255, 255, 1.0) range:NSMakeRange(22, 8)];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.paragraphSpacing = QY_1PX;
        paragraphStyle.lineSpacing = QY_1PX;
        paragraphStyle.minimumLineHeight = 14;
        paragraphStyle.paragraphSpacingBefore = QY_1PX;
        [att addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, str.length)];
        _textView.linkTextAttributes = @{NSForegroundColorAttributeName: RGBA(255, 255, 255, 1.0)};

        _textView.delegate = self;
        _textView.editable = NO;        //必须禁止输入,否则点击将弹出输入键盘
        _textView.scrollEnabled = NO;
        
        _textView.attributedText = att;
//        _textView.userInteractionEnabled = YES;
        _textView.font = [UIFont systemFontOfSize:12];
        _textView.textAlignment = NSTextAlignmentLeft;
    }
    return _textView;
}

3.添加点击回调事件:

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"privacyPolicy"]) {
        //《隐私政策》
        NSLog(@"点击了《隐私政策》");
        return NO;
    }else if ([[URL scheme] isEqualToString:@"userProtocol"]) {
        //《用户隐私》
        NSLog(@"点击了《用户隐私》");
        return NO;
    }else if ([[URL scheme] isEqualToString:@"mobileProtocol"]) {
    //《移动认证服务条款》
    NSLog(@"点击了《移动认证服务条款》");
    return NO;
    }
    return YES;
}
目录
相关文章
|
IDE 开发工具 Windows
鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之Search组件
鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之Search组件
1042 0
|
存储 iOS开发
SwiftUI极简教程17:Gestures手势的使用
SwiftUI极简教程17:Gestures手势的使用
1356 0
SwiftUI极简教程17:Gestures手势的使用
|
iOS开发
iOS调整导航条BarButtonItem与titleView 的间距
iOS调整导航条BarButtonItem与titleView 的间距
747 0
iOS调整导航条BarButtonItem与titleView 的间距
|
机器学习/深度学习 算法 机器人
Windows anaconda python 3.9环境安装dlib
Windows anaconda python 3.9环境安装dlib
3410 0
Windows anaconda python 3.9环境安装dlib
|
5月前
|
安全 Java API
Java Web 在线商城项目最新技术实操指南帮助开发者高效完成商城项目开发
本项目基于Spring Boot 3.2与Vue 3构建现代化在线商城,涵盖技术选型、核心功能实现、安全控制与容器化部署,助开发者掌握最新Java Web全栈开发实践。
561 1
微信分享报错 wxlog:Error:fail to load Keychain status:-25300 解决办法
微信分享报错 wxlog:Error:fail to load Keychain status:-25300 解决办法
3821 0
|
安全 Windows
win10系统:局域网下共享文件夹设置,解决其他电脑访问不成功问题
这篇文章是关于如何在Windows 10系统下设置局域网共享文件夹,并解决其他电脑访问不成功的问题的详细指南。
51855 7
win10系统:局域网下共享文件夹设置,解决其他电脑访问不成功问题
手势代理 shouldBeRequiredToFailByGestureRecognizer 和 shouldRequireFailureOfGestureRecognizer 的区别
手势代理 shouldBeRequiredToFailByGestureRecognizer 和 shouldRequireFailureOfGestureRecognizer 的区别
519 10
无影云电脑产品使用黑神话悟空之在哪里开启云存档
本文介绍了无影云电脑在黑神话悟空游戏中的云存档开启方法,以及针对Switch手柄支持、Xbox360手柄连接问题、Mac客户端鼠标卡顿与旋转问题的解答。提供了具体解决步骤及更多详情链接。