UITextView实现多链接文本

咋地.png

一、前言

废话不说,想实现的效果如下图,使用UITextView实现链接点击跳转的效果,UILabel可以实现效果,但是不支持部分文字点击效果。(支持方法比较麻烦)


UITextView多链接

二、实现方法

1、在NSMutableAttributedString创建extension,用来创建连接文本和对应的链接

extension NSMutableAttributedString {
    public func addLink(_ source: String, link: String, attributes: [String : Any]? = nil) {
        let linkString = NSMutableAttributedString(string: source, attributes: attributes)
        let range: NSRange = NSRange(location: 0, length: linkString.length)
        linkString.beginEditing()
        linkString.addAttribute(NSLinkAttributeName, value: link, range: range)
        linkString.endEditing()
        self.append(linkString)
    }
    
    public func append(_ string: String, attributes: [String : Any]? = nil) {
        let attrString = NSAttributedString(string: string, attributes: attributes)
        self.append(attrString)
    }
}

2、创建UITextView,设置attributeStirng,实现代理
UITextViewDelegate.

view.delegate = USLinkTextManger.instance
            view.isEditable = false
            let attributes = [NSForegroundColorAttributeName: USColor.c301.color,NSFontAttributeName: USFont.t05.font]
            let attrString = NSMutableAttributedString()
            attrString.append(NSAttributedString(string: "有疑问,欢迎拨打", attributes: attributes))
            attrString.addLink("17711678021", link: "protocol1://", attributes: attributes)
            attrString.append(NSAttributedString(string: "也可以通过邮箱联系我们", attributes: attributes))
            attrString.addLink("xiniu@wtest.com", link: "protocol2://", attributes: attributes)
            view.attributedText = attrString

实现代理,协议是自定义的

@available(iOS 10.0, *)
    public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
guard let scheme = url.scheme else {
                return false
            }
            switch scheme {
            case "protocol1":
                print("protocol1")
            case "protocol2":
                print("protocol2")
            default:
                break
            }
            
            return true
    }
    
    public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
guard let scheme = url.scheme else {
                return false
            }
            switch scheme {
            case "tel":
                print("tel")
            case "mail":
                print("mail")
            default:
                break
            }
            
            return true
    }

在代理中,根据自己定义的协议实现不同的方法,可以跳转到原生,也可以新开h5页面

三、需要注意

1、如果需要自定义链接颜色,可以使用UITextView的linkTextAttributes属性

view.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]]
2、如果是自定义链接,UITextView应该禁止编辑,使用isEditable属性.禁止上下滚动使用isScrollEnable

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,803评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,032评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 我要说的是一个真实的故事,真的,真的不能再真的故事。 两个人一间办公室,两个人年纪相仿,志趣相投,当然一拍即合,无...
    d2bf60b960e7阅读 396评论 0 0
  • 很高兴很加入百人计划,并参与了第一次活动。收获颇多,先作个小小的总结。 老徐从五个方面讲解:简历,面试,沟通,测试...
    康宸61阅读 995评论 1 50