Swift UITextView设置富文本点击, 取消一切点击事件(放大镜/复制粘贴/删除等等)

摘要:
(coder:NSCoder){fatalError(“init(coder:)尚未实现”)}}extensionTGSLoginAgreementView{functextViewShouldBeginEditing(_textView:UITextView)-˃Bool{returnfalse}functtextView(_textView:UITextView,should InteractiveWithURL:URL,in字符范围:NSRange,交互:UITextItemInteraction)-˃Bool{ifURL.scheme==“userProtocol”{self.clickHandle?(.userProtocol)returnfalse}elseifURL。scheme==“privacyPolicy”{self.clickHandle?

Swift UITextView设置富文本点击, 取消一切点击事件(放大镜/复制粘贴/删除等等)第1张

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  

免责声明:文章转载自《Swift UITextView设置富文本点击, 取消一切点击事件(放大镜/复制粘贴/删除等等)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇获取JAVA对象占用的内存大小vue中添加文字或图片水印下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

iOS UI-文本视图(UITextView)

1 #import "ViewController.h" 2 3 @interface ViewController ()<UITextViewDelegate> 4 5 @property (strong, nonatomic) UITextView *textView; 6 7 @end 8 9 @implement...

UITextView in iOS7 doesn&amp;amp;#39;t scroll

UITextView in iOS7 has been really weird. As you type and are entering the last line of your UITextView, the scroll view doesn't scroll to the bottom like it should and it cause...

UITextView 和 UITextField限制字符数和表情符号

UITextField限制字符数 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ int pMaxLength = 12; NSInteg...