zoukankan      html  css  js  c++  java
  • Swift UITextView设置富文本点击, 取消一切点击事件(放大镜/复制粘贴/删除等等)

    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  

  • 相关阅读:
    java.sql.SQLException: Access denied for user 'root'@'10.1.0.2' (using password: YES)
    在eclipse中安装使用lombok插件
    Socket编程实践(5) --TCP粘包问题与解决
    Socket编程实践(8) --Select-I/O复用
    Socket编程实践(6) --TCP服务端注意事项
    Socket编程实践(4) --多进程并发server
    Socket编程实践(3) --Socket API
    Socket编程实践(2) --Socket编程导引
    Socket编程实践(1) --TCP/IP简述
    Socket编程实践(11) --epoll原理与封装
  • 原文地址:https://www.cnblogs.com/qingzZ/p/14009967.html
Copyright © 2011-2022 走看看