iOS开发笔记-88:Swift5 文字中添加链接(NSMuta

2019-04-13  本文已影响0人  原味蛋炒饭
        let ruleAstr = NSMutableAttributedString.init(string: "登录及代表阅读并同意服务条款")
        let pstyle = NSMutableParagraphStyle()
        pstyle.alignment = NSTextAlignment.center
        ruleAstr.addAttributes([NSAttributedString.Key.paragraphStyle : pstyle], range: NSRange(location: 0, length: ruleAstr.length))
        
        ruleAstr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.lightGray], range: NSRange(location: 0, length: ruleAstr.length))
        ruleAstr.addAttributes([NSAttributedString.Key.font : FONT12], range: NSRange(location: 0, length: ruleAstr.length))
        
        ruleAstr.addAttributes([NSAttributedString.Key.foregroundColor : kMainBlueColor], range: ruleAstr.mutableString.range(of: "服务条款"))
        ruleAstr.addAttribute(NSAttributedString.Key.link, value: URL(string: "ruleAct")!, range: ruleAstr.mutableString.range(of: "服务条款"))
        ruleT.attributedText = ruleAstr
        ruleT.isEditable = false
        ruleT.delegate = self
extension LoginVC: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        if URL.absoluteString == "ruleAct" {
            JJLog("点击条款")
        }
        return false
    }
}
上一篇下一篇

猜你喜欢

热点阅读