Swift 运用传统NSAttributedString与YYTextContainer 封装调用 (2)

125 阅读1分钟

高级进阶:一个懒加载方法搞定啦

 lazy var privacyView: UIView = {
     
     let view = UIView(frame: CGRect(x: 30, y:  Screen.height - 100, width: Screen.width - 60, height: 20))
     view.backgroundColor = UIColor.clear
           
     var text = NSMutableAttributedString.init(string: "同意《用户协议》《隐私条款》")
     text.color  = UIColor.rgb(250, 217, 75)
     text.font   = UIFont.init(name: "PingFangSC-Regular", size: 14)
     text.minimumLineHeight = 26
     
     let protocolRange = (text.string as NSString).range(of: "《用户协议》")
     text.setTextHighlight(protocolRange, color: UIColor.rgb(165, 212, 251), backgroundColor: .clear) { UIView, NSAttributedString, NSRange, CGRect in
         YJRoute.toAgreementVC()
     }

     let clauseRange = (text.string as NSString).range(of: "《隐私条款》")
     text.setTextHighlight(clauseRange, color: UIColor.rgb(165, 212, 251), backgroundColor: .clear) { UIView, NSAttributedString, NSRange, CGRect in
         YJRoute.toPrivacyVC()
     }
     
     let container               = YYTextContainer.init(size: CGSize(width: YJScreen.width - 60 , height: CGFloat(MAXFLOAT)))
     let layout                  = YYTextLayout.init(container: container, text: text)
     let containerLabel          = YYLabel.init()
     containerLabel.size         = layout!.textBoundingSize
     containerLabel.origin       = CGPoint(x: 0, y: 0)
     containerLabel.textLayout   = layout!
     
     view.addSubview(containerLabel)
     return view
     
 }()