当前位置: 首页 > news >正文

iOS textView支持超链接跳转

将某些文字变成高量可以点击的超链接核心功能代码

        attri.addAttribute(NSAttributedString.Key.link, value:NSURL.init(string: "dctt:p/userPrivacy.html")!, range: NSRange.init(location: s.count - 4, length: 4) )textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor:UIColorFromHex(rgbValue: 0xff4500) ]

同时,要实现点击超链接的UITextView代理方法

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction API_AVAILABLE(ios(10.0));

完整代码

   let s = "本《隐私概要》将向你说明:\n1.为了帮助你浏览、发布信息、评论交流、注册认证,我们会收集你的部分必要信息;\n\n2.为了提供以上服务,我们可能会收集联络方式、位置、通讯录等部分敏感信息,你有权拒绝或撤销授权;\n\n3.未经你同意,我们不会从第三方获取、共享或提取你的信息;\n\n4.你可以访问、更正、删除你的个人信息,我们也将提供注销、投诉的方式。如果你点击不同意,我们将仅收集浏览内容所必须的信息,但发布信息、交流评论可能会受到影响。\n\n前往查看完整的 隐私政策"let attri = NSMutableAttributedString.init(string: s)//        attri.addAttributes([NSForegroundColorAttributeName:UIColor.red ], range: NSRange.init(location: s.count - 4, length: 4))attri.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 15), range: NSRange.init(location: 0, length: s.count))attri.addAttribute(NSAttributedString.Key.link, value:NSURL.init(string: "dctt:p/userPrivacy.html")!, range: NSRange.init(location: s.count - 4, length: 4) )textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor:UIColorFromHex(rgbValue: 0xff4500) ]textView.attributedText = attri;textView.delegate = self

实现代理方法

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {        if let scheme = URL.scheme , scheme == "dctt" {let vc = BaseWebViewController(baseUrl:privacy_agreement_url)vc.title = "隐私政策"self.navigationController?.pushViewController(vc, animated: true)return false}return true}

效果图
请添加图片描述

http://www.lryc.cn/news/126642.html

相关文章:

  • 大牛分析相机镜头光学中疑难问题
  • xacro机器人模型文件转urdf文件怎么编写launch文件启动gazebo仿真和在rviz2内显示模型
  • 前端图片转base64,并使用canvas对图片进行压缩
  • 电脑键盘打不了字按哪个键恢复?最新分享!
  • ue5读取外部文件
  • 【ARM】Day4 点亮LED灯
  • TiDB基础介绍、应用场景及架构
  • 深入学习前端开发,掌握HTML、CSS、JavaScript等技术
  • python编程小游戏 五子棋,python编程小游戏简单的
  • spring ico容器 spring注入方式 spring与tomcat整合
  • ansible 修改远程主机nginx配置文件
  • EV 录屏修复小工具
  • 蓝牙资讯|中国智能家居前景广阔,蓝牙Mesh照明持续火爆
  • 2023年排行前五的大规模语言模型(LLM)
  • DoIP学习笔记系列:(六)满足AES128-CMAC算法的“安全认证”.dll生成实践
  • Collections操作集合的工具类,可变参数、集合操作的工具类
  • Linux Kernel:进程表示
  • 黑马项目一阶段面试58题 苍穹外卖具体技术细节9题
  • SkyEye操作指南:连接TI CCS的IDE调试
  • PINN神经网络源代码解析(pyTorch)
  • ChatGPT​保密吗?它有哪些潜在风险?如何规避?
  • C++中配置OpenCV的教程
  • 收银一体化-亿发2023智慧门店新零售营销策略,实现全渠道运营
  • node.js内置模块fs,path,http使用方法
  • 【git clone error:no matching key exchange method found】
  • 谈谈网络协议的定义、组成和重要性
  • ssh免密登陆报错ERROR: @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  • 【kubernetes】Pod控制器
  • aspose.ocr 的图片识别
  • 安卓纯代码布局开发游戏二:Android Studio开发环境搭建