创建内联文本链接,该链接将在我的应用程序中打开另一个视图

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

我正在尝试在我的应用程序中创建一个显示一大块文本的视图。我希望该文本中的某些单词成为可打开另一个视图的可单击链接。我已经谷歌搜索了很长时间,似乎找不到正确的方法。

仅仅尝试做像

HStack{ Text("example text") Button("word"), action) Text("rest of example text") }
这样的事情是行不通的,因为它真的搞乱了文本块的整体布局。

我也在研究属性字符串。我想知道是否可以创建一个自定义 url 来指向我想要的应用程序中的视图,然后将其应用到属性字符串。这可能吗?

这里最好的方法是什么?

当我尝试打开处理单击的网址内的视图时,没有任何反应。难道就不能这样做吗?我需要导航到另一个 swiftUi 视图吗?

struct shortStoryView: View {
    @State var showPopUpScreen: Bool = false
    
    var body: some View {
        GeometryReader{ geo in
            ZStack{
                Image("homeWallpaper")
                    .resizable()
                    .scaledToFill()
                    .edgesIgnoringSafeArea(.all)
                    .frame(width: geo.size.width, height: geo.size.height, alignment: .center)
                    .opacity(1.0)
                
                testStory(showNewScreen: self.$showPopUpScreen).frame(width: 350, height:500).background(Color.white.opacity(0.75)).cornerRadius(20).overlay( RoundedRectangle(cornerRadius: 16)
                    .stroke(.gray, lineWidth: 6))
                .shadow(radius: 10)
                
            }
        }
    }
    
    struct testStory: View{
        @Binding var showNewScreen: Bool
        
        var body: some View{
            ScrollView(.vertical, showsIndicators: true) {
                    Text("Cristoforo Colombo [nasce](myappurl://action) nel 1451 vicino a Genova, nel nord Italia. A 14 anni diventa marinaio e viaggia in numerosi Paesi. Per Cristoforo Colombo la Terra è rotonda e verso la fine del ‘400, vuole viaggiare verso l’India e vuole farlo con un viaggio verso ovest. La spedizione è costosa e Colombo prima chiede aiuto al re del Portogallo e poi alla regina Isabella di Castiglia. Nel 1492, dopo mesi di navigazione, scopre però un nuovo continente: l’America, che viene chiamata il Nuovo Mondo. Cristoforo Colombo fa altri viaggi in America ma ormai non è più così amato e così muore nel 1506 povero e dimenticato da tutti.")
                        .modifier(textModifer())
                        .environment(\.openURL, OpenURLAction { url in
                            handleURL(url)
                        })
                }
            }
        
        func handleURL(_ url: URL) -> OpenURLAction.Result {
            popUpView()
            return .handled
        }
    }
    
    struct popUpView: View{
        @Environment(\.presentationMode) var presentationMode
        
        var body: some View{
            ZStack(alignment: .topLeading) {
                Text("Nascire - To be Born")
                    .font(Font.custom("Arial Hebrew", size: 20))
                    .background(Color.white.opacity(0.5))
                
                Button(action: { presentationMode.wrappedValue.dismiss()},
                       label: {
                    Image(systemName: "xmark")
                        .foregroundColor(.white)
                        .font(.largeTitle)
                })
            }
        }
    }
swiftui nsattributedstring custom-url
1个回答
0
投票

这并不完全是您正在寻找的,但这是我作为解决方法所做的。

NavigationLink {
    RandomView()
} label: {
    Group {
        Text("Your text")
            .font(.subheadline)
            .foregroundStyle(Color.black)
            
        + Text("More text")
            .font(.subheadline)
            .foregroundStyle(Color(.systemBlue))
    }
    .multilineTextAlignment(.leading)
}

我将整个文本设为导航链接。我不知道如何只创建一个 Text() 导航链接,除非您想将它们并排放在 hstack 中,但如果用户设备很小或文本很长并且溢出,这可能看起来很奇怪。

© www.soinside.com 2019 - 2024. All rights reserved.