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  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM