UITextView设置文本超链接

2022-04-08  本文已影响0人  哥只是个菜鸟
private lazy var googleAccountView: UITextView = {
        let textView = UITextView()
        textView.isEditable = true
        textView.isScrollEnabled = false
        textView.delegate = self
        textView.backgroundColor = .clear
        textView.textContainerInset = .zero
        let string = "You can unlink anytime at Google Account."
        let tipString = "Google Account."
        let font = UIFont.systemFont(ofSize: 12)

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 3
        let prefixAttributedText = NSMutableAttributedString(string: string, attributes: [.font: font, .foregroundColor: UIColor.black, .paragraphStyle: paragraphStyle])
        let suffixAttributedText = NSMutableAttributedString(string: tipString, attributes: [.link: ClickJumpType.googleAccount.rawValue, .underlineStyle: NSUnderlineStyle.single.rawValue])
        textView.linkTextAttributes = [.foregroundColor: UIColor(hex: 0x007DFF),
                                       .underlineColor: UIColor(hex: 0x007DFF),
                                       .font: font]
        prefixAttributedText.append(suffixAttributedText)
        textView.attributedText = prefixAttributedText
        return textView
    }()
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { 
        return false
    }
上一篇 下一篇

猜你喜欢

热点阅读