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

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

作者: 原味蛋炒饭 | 来源:发表于2019-04-13 14:36 被阅读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
        }
    }
    

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/gjnlwqtx.html