UITextView 避免文本选择,但保留可点击链接

UITextView avoid text selection, but keep tappable links

我有一个弹出窗口 window,显示用户的服务条款和隐私政策,如何禁用文本选择但保持 link 可点击。 screenshot 如果我设置: 已解决:通过在 UIViewDelegate 中添加 textViewDidChangeSelection 方法并设置

textView.isSelectable = false
textView.isUserInteractionEnabled = true
textView.isSelectable = true

链接变得不可交互。 如何保持 link 可点击且文本不可选择?

我的全部class:

//  HyperLinkTextView.swift

import SwiftUI

struct Hyperlink {
    var word: String
    var url: NSURL
}

struct HyperLinkTextView: UIViewRepresentable {
    
    private var text: String
    private var links: [Hyperlink]
    let textView = UITextView()
    
    init(text: String, links: [Hyperlink]) {
        self.text = text
        self.links = links
    }
    
    func makeUIView(context: Self.Context) -> UITextView {
        let attributedString = NSMutableAttributedString(string: text)
        links.forEach { hyperlink in
            let linkAttributes = [NSAttributedString.Key.link: hyperlink.url]
            
            var nsRange = NSMakeRange(0, 0)
            if let range = text.range(of: hyperlink.word) {
                nsRange = NSRange(range, in: text)
            }
            
            attributedString.setAttributes(linkAttributes, range: nsRange)
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSNumber(value: 1), range: nsRange)
        }
        
        textView.isEditable = false
        textView.delegate = context.coordinator
        textView.attributedText = attributedString
        textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(Theme.colorClickables)]
        textView.isUserInteractionEnabled = true
        textView.isSelectable = true
    
        return textView
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.font = UIFont(name: "ArialMT", size: 18)
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }
    
    
    public class Coordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegate {
        
        weak var textView: UITextView?
            
        public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction, replacementText text: String) -> Bool {
            return true
        }
        
        func textViewDidChangeSelection(_ textView: UITextView) {
            if textView.selectedTextRange != nil {
                textView.delegate = nil
                textView.selectedTextRange = nil
                textView.delegate = self
            }
        }
    }
}

添加此 UITextViewDelegate textViewDidChangeSelection 并注释掉 isEditableisSelectable:

func textViewDidChangeSelection(_ textView: UITextView) {
    if textView.selectedTextRange != nil {
        textView.delegate = nil
        textView.selectedTextRange = nil
        textView.delegate = self
    }
}