SwiftUI onTapGesture取消属性字符串链接点击

wrrgggsh  于 2023-01-12  发布在  Swift
关注(0)|答案(1)|浏览(134)

我有一个AttributedString,在UI中有一个链接,如下所示:
结构内容视图:查看{

private let attributedString: AttributedString = {
    var attributedString = AttributedString("Notifications disabled. Please enable them in Settings.")
    attributedString.foregroundColor = .black

    if let range = attributedString.range(of: "Settings") {
        attributedString[range].foregroundColor = .blue
        attributedString[range].link = .init(string: UIApplication.openSettingsURLString)
    }

    return attributedString
}()

var body: some View {
    ZStack {
        Color.white
            .ignoresSafeArea()
            .onTapGesture {
                print("Tapped")
            }

        Text(attributedString)
    }
}

由于某种原因,onTapGesture阻止了点击手势的发生。我也不能选择将点击手势移动到ZStack,因为这会阻止主应用中的其他手势。我该如何解决这个问题?

agxfikkp

agxfikkp1#

你可以试试这个

Text(.init(text)) { word in
                    if let range = word.range(of: "click here.") {
                        word[range].underlineStyle = .single
                        word[range].link = URL(string: "https://aspas.notion.site/Cue-Placeholders-64a63f03a7794d00bdf3f077c4ff5723")
                    }
                }

extension Text {

init(_ string: String, configure: ((inout AttributedString) -> Void)) {
    var attributedString = AttributedString(string) /// create an `AttributedString`
    configure(&attributedString) /// configure using the closure
    self.init(attributedString) /// initialize a `Text`
} }

相关问题