这是一段代码,一半已被 comments , 在第一部分中,数组中的文本元素位于VStack中,并且每个元素都出现在新行中.但单击它时会显示有关单击的数组元素的详细信息. 但在第二部分中,文本在段落中连接并可见.但单击后,它无法显示所单击元素的详细信息. 我只需要文本的段落表示和每个元素可点击. 这里是代码

//import SwiftUI
//
//struct ContentView: View {
//    let dummyStrings = [
//        "Lorem ipsum dolor sit amet*",
//        "consectetur adipiscing elit*",
//        "sed do eiusmod tempor incididunt*",
//        "ut labore et dolore magna aliqua*",
//        "Ut enim ad minim veniam*",
//        "quis nostrud exercitation ullamco*",
//        "laboris nisi ut aliquip ex ea commodo consequat*",
//        "Duis aute irure dolor in reprehenderit*",
//        "in voluptate velit esse cillum dolore*",
//        "eu fugiat nulla pariatur*"
//    ]
//    
//    var body: some View {
//        ScrollView {
//            VStack(alignment: .leading, spacing: 10) {
//                // Concatenate all strings into a single continuous text
//                Text(dummyStrings.joined(separator: " "))
//                    .font(.body)
//                    .foregroundColor(.blue)
//                    .modifier(ClickableText(lines: dummyStrings) { string in
//                        print("Clicked:", string)
//                    })
//            }
//            .padding()
//        }
//    }
//}
//
//// View modifier to make each line of text individually tappable
//struct ClickableText: ViewModifier {
//    let lines: [String]
//    let action: (String) -> Void
//    
//    func body(content: Content) -> some View {
//        // Use GeometryReader to determine the size of each line of text
//        GeometryReader { proxy in
//            VStack(spacing: 0) {
//                ForEach(lines, id: \.self) { line in
//                    Text(line)
//                        .onTapGesture {
//                            action(line)
//                        }
//                        .frame(width: proxy.size.width, alignment: .leading)
//                        .contentShape(Rectangle()) // Make whole Text area tappable
//                }
//            }
//        }
//    }
//}
//
//struct ContentView_Previews: PreviewProvider {
//    static var previews: some View {
//        ContentView()
//    }
//}
//

//------------------For Testing use -------------------

import SwiftUI

struct ContentView: View {
    let dummyStrings = [
        "1Lorem ipsum dolor sit amet",
        "2consectetur adipiscing elit",
        "3sed do eiusmod tempor incididunt",
        "4ut labore et dolore magna aliqua",
        "5Ut enim ad minim veniam",
        "6quis nostrud exercitation ullamco",
        "7laboris nisi ut aliquip ex ea commodo consequat",
        "8Duis aute irure dolor in reprehenderit",
        "9in voluptate velit esse cillum dolore",
        "10 eu fugiat nulla pariatur"
    ]
    
    var body: some View {
        let concatenatedText = dummyStrings.joined(separator: " ")
        
        return ScrollView {
            Text(concatenatedText)
                .font(.body)
                .foregroundColor(.blue)
                .lineSpacing(5)
                .onTapGesture {
                    // Handle tap gesture here (e.g., print tapped portion)
                    print("Text tapped")
                }
                .padding()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

 

推荐答案

一种方法是将所有字符串连接到AttributedString中,其中每个字符串都有一个与之关联的link.link将是URL,其中包含您想要编码的任何信息.然后,您可以拦截openURL动作并运行您想要的任何代码.

例如,如果您想将字符串本身编码为URL的路径

func encodeIntoAttributedString(_ str: String) -> AttributedString {
    var attrStr = AttributedString(str)
    attrStr.link = URL(filePath: str)
    return attrStr
}

使用类似的东西将所有内容结合在一起:

extension Array where Element == AttributedString {
    func joined(separator: AttributedString = "") -> AttributedString {
        guard let first else { return "" }
        var result = first
        for elem in self[1...] {
            result += separator
            result += elem
        }
        return result
    }
}

用途:

let text = [
    "1Lorem ipsum dolor sit amet",
    "2consectetur adipiscing elit",
    "3sed do eiusmod tempor incididunt",
    "4ut labore et dolore magna aliqua",
    "5Ut enim ad minim veniam",
    "6quis nostrud exercitation ullamco",
    "7laboris nisi ut aliquip ex ea commodo consequat",
    "8Duis aute irure dolor in reprehenderit",
    "9in voluptate velit esse cillum dolore",
    "10 eu fugiat nulla pariatur"
].map(encodeIntoAttributedString).joined(separator: " ")

var body: some View {
    ScrollView {
        Text(text)
            // these modifiers could also be replaced by adding the corresponding attributes in encodeIntoAttributedString
            .font(.body)
            .foregroundColor(.blue)
            .lineSpacing(5)
            .padding()
            .environment(\.openURL, OpenURLAction { x in
                // here I am printing the string itself, which is encoded in the path
                print(x.path(percentEncoded: false))
                return .handled
            })
    }
}

Ios相关问答推荐

SwiftUI SF Symbols图像:暗模式图像

自定义油漆圆角三角形

由于代码签名时出错,无法在iOS设备上发布iOS Maui Build

自定义UIControl不适用于UITapGestureRecognizer

iOS应用程序冻结在UICollectionView代码,但我没有';没有任何UICollectionViews

在 React Native iOS 中加载多个 JS 包

当任何 TextEditor 有多于一行时,所有 TextEditor 都会调整大小

React Native IOS base64 编码图像不显示

SwiftUI:如何为 ScrollView 制作可拉伸(灵活)的粘性标题?

在视图控制器中实例化视图控制器是否可能/智能?

访问未定义的 stage_in 金属着色器参数

在 tableview 中下载图像缩略图时如何提高滚动性能

如何快速设置条形按钮的图像?

iPhone 6 和 6 Plus 媒体查询

ARKit vs. ARCore vs. Vuforia vs. D'Fusion Mobile vs. Layar SDK

如何使用 Swift 从assets资源 中加载特定图像

如何在react 导航中获取当前路由名称?

iOS 7 圆形框架按钮

无效的 iPhone 应用程序二进制文件

为所有 UIImageViews 添加圆角