UITextView延迟的AttributedString链接SwiftUI

问题描述 投票:0回答:1

我的文本视图看起来像这样:

class StudyText: UITextView,  UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        print(URL)

        return false
    }

    override var canBecomeFirstResponder: Bool {
        return false
    }
}

这是结构:

struct ClickableText: UIViewRepresentable {
    @Binding var text: NSMutableAttributedString

    func makeUIView(context: Context) -> StudyText {
        let view = StudyText()

        view.dataDetectorTypes = .all
        view.isEditable        = false
        view.isSelectable      = true
        view.delegate          = view
        view.isUserInteractionEnabled = true

        return view
    }

    func updateUIView(_ uiView: StudyText, context: Context) {
        uiView.attributedText = text

    }

}

而且我正在使用属性链接。

我尝试过的每个解决方案都不能使链接快速响应。立即。在显示print语句之前需要一些延迟。

我尝试过:

view.delaysContentTouches = false

我尝试过:

let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tappedTextView(tapGesture:)))
self.addGestureRecognizer(tapRecognizer)


@objc func tappedTextView(tapGesture: UIGestureRecognizer) {

    let textView = tapGesture.view as! UITextView
    let tapLocation = tapGesture.location(in: textView)
    let textPosition = textView.closestPosition(to: tapLocation)
    let attr = textView.textStyling(at: textPosition!, in: .forward)!

    if let url: URL = attr[NSAttributedString.Key(rawValue: NSAttributedString.Key.link.rawValue)] as? URL {
        print("clicking here: \(url)")

    }

}

但是他们都没有工作。它总是会延迟响应我该如何解决?

ios swift swiftui uitextview uiviewrepresentable
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.