UITextView设置文本超链接

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
    }()
  • 禁用Textview的复制剪切等操作isEditable设置为true, 同时textViewShouldBeginEditing返回false
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { 
        return false
    }

你可能感兴趣的:(UITextView设置文本超链接)