• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

 

 

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()
        }
    }
}

 

 

参考:

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  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift学习-09--枚举发布时间:2022-07-13
下一篇:
swift:创建滚动视图的图片轮播器发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap