Swift YYLabel 高亮 点击事件

class MdyAgreeView: UIView,Nibloadable {
    let textContent:String = "请仔细阅读《用户服务条款》和《用户隐私政策》我们将按照您同意的条款使用您的个人信息,以便为您提供服务,我们尽全力保护您的个人信息安全如您同意此政策,请点击“同意”并开始使用我们的产品和服务。"
    let text1:String = "《用户服务条款》"
    let text2:String = "《用户隐私政策》"
    @IBOutlet weak var agreeLab: YYLabel!
    
    @IBOutlet weak var agreeLabH: NSLayoutConstraint!
    
    override func awakeFromNib() {
        super.awakeFromNib()
     
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        protocolIsSelect()
    }
    
    func protocolIsSelect() {
        agreeLabH.constant =    textContent.getBoundingFontHeight(rectWidth:agreeLab.frame.width,font: gsfont(18),lineSpacing: 8)
        agreeLab.textAlignment = .left
        agreeLab.numberOfLines = 0
        agreeLab.textColor = colorFromHEX(colorVaule: "#616161")
        
        let text = NSMutableAttributedString(string:textContent )
        /// 3.设置高亮
        if let range1 = textContent.range(of: text1) {
            let nsRange = NSRange(range1, in: textContent)
            text.yy_setTextHighlight(nsRange, color: UIColor.gsSelectColor, backgroundColor: .clear) { view, str, ran, frame in
                /// "用户协议"
                let webvc = GSSearviceWebVC()
                
                webvc.url = User_Registration_Agreement
                webvc.titleStr = "服务条款"
                webvc.modalPresentationStyle = .fullScreen
                
                getCurrentVC().present(webvc, animated: true)
            }
        }
        
        if let range2 = textContent.range(of: text2) {
            let nsRange = NSRange(range2, in: textContent)
            text.yy_setTextHighlight(nsRange, color: UIColor.gsSelectColor, backgroundColor: .clear) { view, str, ran, frame in
                let webvc = GSSearviceWebVC()
                
                webvc.url = User_Privacy_Agreement
                webvc.titleStr = "服务隐私政策"
                webvc.modalPresentationStyle = .fullScreen
                
                getCurrentVC().present(webvc, animated: true)
                
            }
        }
       
        text.yy_lineSpacing = 8
        text.yy_font =  gsfont(18)
        agreeLab.attributedText = text
           // 居中显示一定要放在这里,放在viewDidLoad不起作用
        
        
       }
    
    @IBAction func agree(_ sender: Any) {
        userDefault.set("12", forKey: "isAgree")
        self.removeFromSuperview()
    }
    
    @IBAction func notagree(_ sender: Any) {
        exit(0)
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值