1. 设置富文本,超链接点击
2. 取消一切点击事件(放大镜/复制粘贴/删除等等)
/// 同意协议view class TGSLoginAgreeView: UIView, UITextViewDelegate { ///点击类型 enum ClickLinkType { ///用户协议 case userProtocol ///隐私条款 case privacyPolicy } ///点击事件 var clickHandle:((_ clickType:ClickLinkType)->())? ///同意View private lazy var agreeTextView : UITextView = { let textStr = "登录既代表您已同意《用户协议》和《隐私条款》" let textView = UITextView() textView.delegate = self textView.font = TGSPingFangFontTool.getPingFangFont(13, .regular) textView.textColor = UIColor.colorWithHexString("#666666") textView.textAlignment = .center ///设为true 在代理里面禁掉所有的交互事件 textView.isEditable = true textView.autoresizingMask = UIView.AutoresizingMask.flexibleHeight textView.isScrollEnabled = false let attStr = NSMutableAttributedString(string: textStr) //点击超链接 attStr.addAttribute(NSAttributedString.Key.link, value: "userProtocol://", range: (textStr as NSString).range(of: "《用户协议》")) //点击超链接 attStr.addAttribute(NSAttributedString.Key.link, value: "privacyPolicy://", range: (textStr as NSString).range(of: "《隐私条款》")) textView.attributedText = attStr ///只能设置一种颜色 textView.linkTextAttributes = [ NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString("#FF4555") ] return textView }() override init(frame: CGRect) { super.init(frame: frame) configUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension TGSLoginAgreeView{ func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { return false } func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { if URL.scheme == "userProtocol"{ self.clickHandle?(.userProtocol) return false }else if URL.scheme == "privacyPolicy"{ self.clickHandle?(.privacyPolicy) return false } return true } } extension TGSLoginAgreeView{ private func configUI(){ ///同意view self.addSubview(agreeTextView) agreeTextView.snp.makeConstraints { (make) in make.edges.equalToSuperview() } } }
参考:
UITextView禁用复制粘贴放大
https://blog.csdn.net/Lu_Ca/article/details/53744938?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2.not_use_machine_learn_pai&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2.not_use_machine_learn_pai