做項(xiàng)目時(shí)用了UITextView加鏈接的方式,做文本的點(diǎn)擊事件
let ruleStr = "I have read and accepted privacy policy and service agreement"
ruleAttr.addAttribute(NSAttributedString.Key.link, value: "privacy://", range: NSRange(location: 25, length: 14))
ruleAttr.addAttribute(NSAttributedString.Key.link, value: "sagreement://", range: NSRange(location: 44, length: 17))
self.privacyAgreementTV.delegate = self
self.privacyAgreementTV.isEditable = true self.privacyAgreementTV.isScrollEnabled = false
self.privacyAgreementTV.attributedText = ruleAttr
self.privacyAgreementTV.font = UIFont.systemFont(ofSize: 12)
self.privacyAgreementTV.linkTextAttributes = [.foregroundColor:main_color]
extension MyController: UITextViewDelegate {
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.scheme == "privacy" {
KYTool.ky_showText(textStr: "123")
}else if URL.scheme == "sagreement" {
KYTool.ky_showText(textStr: "456")
}
return true
}
}
到此点晴,基本功能大致完成傻寂,如果有想改進(jìn)的自行查閱其他內(nèi)容
重點(diǎn)是:長(zhǎng)按事件屏蔽融蹂,遍歷了一下UITextView的事件拟烫,高達(dá)20多種疆前,突發(fā)奇想,手動(dòng)刪除不要的事件,如下
//遍歷所有手勢(shì)爷辙,留下點(diǎn)擊的手勢(shì)
for ges in privacyAgreementTV.gestureRecognizers! {
if !(ges is UITapGestureRecognizer) { //如果不是單擊手勢(shì),全部刪除
privacyAgreementTV.removeGestureRecognizer(ges)
}
}
齊活1蚧怠!膝晾!