實(shí)現(xiàn)以上紅框內(nèi)的效果练对,設(shè)置部分字體內(nèi)容的顏色為紅色来农,前面按鈕可以點(diǎn)擊,紅色字體內(nèi)容也可以點(diǎn)擊:
1乘盼、導(dǎo)入YYText:
import YYText
2升熊、定義YYLabel屬性
var protocolLabel = YYLabel()
3、代碼實(shí)現(xiàn)
func initUI() {
self.protocolLabel.font = UIFont.systemFont(ofSize: 12.0)
? ? ? ? self.protocolLabel.textColor = UIColor.init(rgb: 0x161616)
? ? ? ? self.protocolLabel.textAlignment = .center
? ? ? ? self.protocolLabel.isUserInteractionEnabled = true
? ? ? ? view.addSubview(self.protocolLabel)
? ? ? ? self.protocolLabel.snp.makeConstraints { (make) in
? ? ? ? ? ? make.top.equalTo(vercodeUnderLineView.snp.bottom).offset(15.cgFloat.pad(20).adaptHeight())
? ? ? ? ? ? make.left.equalTo(vercodeUnderLineView)
? ? ? ? ? ? make.right.equalToSuperview().offset(-45.cgFloat.pad(-211).adaptWidth())
? ? ? ? }
? ? ? ? self.protocolLabel.numberOfLines = 2
? ? ? ??//設(shè)置協(xié)議
? ? ? ? setAttribute(markup: "《AI美照館用戶協(xié)議及隱私政策》")
}
? ? //MARK:設(shè)置協(xié)議
? ? func setAttribute(markup:String) {
? ? ? ? let iconImg =UIButton()
? ? ? ? iconImg.setImage(UIImage(named:"ic_radio button_nor"), for: .normal)
? ? ? ? iconImg.setImage(UIImage(named:"ic_radio button_nsel"), for: .selected)
? ? ? ? iconImg.hitEdgeInsets=UIEdgeInsets(top:20, left:20, bottom:20, right:20)
? ? ? ??//MARK:設(shè)置按鈕點(diǎn)擊事件
? ? ? ? iconImg.rx.tap.subscribe(onNext: {_in
? ? ? ? ? ? iconImg.isSelected=!iconImg.isSelected
? ? ? ? }).disposed(by: disposeBag)
? ? ? ? iconImg.frame=CGRect(x:0, y:0, width:15, height:15)
? ? ? ? letatt =YYTextAttachment(content: iconImg)
? ? ? ? att.contentMode= .left
? ? ? ? lettext =String(format:" 點(diǎn)擊登錄即代表同意%@",markup)
? ? ? ? let attributeStr =NSMutableAttributedString.yy_attachmentString(withContent: iconImg, contentMode: .left, attachmentSize:CGSize(width:15, height:15), alignTo:UIFont.systemFont(ofSize:12), alignment: .center)
? ? ? ? attributeStr.yy_insertString(text, at:1)
? ??????//MARK:設(shè)置協(xié)議點(diǎn)擊事件
? ? ? ? attributeStr.yy_setTextHighlight(NSRange(location:11, length: markup.count), color:UIColor.init(rgb:0xF76F00), backgroundColor:nil) { (view, att, range, rect)in
? ? ? ? ? ? print("跳轉(zhuǎn)協(xié)議")
? ? ? ? }
? ? ? ? self.protocolLabel.attributedText = attributeStr
? ? }