在 SwiftUI 中创建一个带有可点击文本的大段落

更新
最近看到富文本在SwiftUI中得到了解决,iOS15中SwiftUI 内置了对渲染 Markdown 的支持,包括粗体、斜体、链接等。它直接内置于 SwiftUI 的Text视图中,如:

VStack {
//            Text("This is regular text.")
//            Text("* This is **bold** text, this is *italic* text, and this is ***bold, italic*** text.")
//            Text("~~A strikethrough example~~")
//            Text("`Monospaced works too`")
//            Text("Visit Apple: [click here](https://apple.com)")
            
            Text("* This is **bold** text, this is *italic* text, and this is ***bold, italic*** text.[click here](https://apple.com) ~~A strikethrough example~~`Monospaced works too`Visit Apple: [click here](https://apple.com)")
        }

效果图


image.png

参考链接:
SOLVED: Is there a way to easily display rich text in SwiftUI?
How to render Markdown content in text


最优方案:(适用于死文案)

HStack {
        Text("You agree to our")

        Text("Terms")
                .onTapGesture {
                         UIApplication.shared.open(URL(string: "https://designcode.io/terms")!)
                }

        Text("and")

        Text("Privacy policy")
                .onTapGesture {
                         UIApplication.shared.open(URL(string: "https://designcode.io/privacy")!)
                }

        Text(".")
}

不得不考虑方案:

import SwiftUI

struct MyParclesView: View {
    let text1 = "Hello $world$ I am %a% &lovely&\n\n~Lynn~"
    
    var body: some View {
        VStack{
            ForEach(text1.split(separator: "\n"), id: \.self) { line in
                HStack(spacing: 4) {
                    ForEach(line.split(separator: " "), id: \.self) { part in
                        self.generateBlock(for: part)
                    }
                }
            }
        }
        
    }
    private func generateBlock(for str: Substring) -> some View {
            Group {
                if str.starts(with: "$") {
                    Text(str.dropFirst().dropLast(1))
                        .bold()
                } else
                if str.starts(with: "&") {
                    Text(str.dropFirst().dropLast(1))
                        .font(Font.body.smallCaps())
                } else
                if str.starts(with: "%") {
                    Text(str.dropFirst().dropLast(1))
                        .italic().foregroundColor(.red)
                } else
                if str.starts(with: "~") {
                    Text(str.dropFirst().dropLast(1))
                        .underline().foregroundColor(.blue).onTapGesture {
                            print("tapping ")
                        }
                } else {
                    Text(str)
                }
            }
        }
}

struct MyParclesView_Previews: PreviewProvider {
    static var previews: some View {
        MyParclesView()
    }
}

效果:


image.png

最优方案:(不适用点击事件)

HStack {
      (
         Text("This is ")
          +
          Text("bold").bold()
          +
         Text(" text.")
      )
}

你可能感兴趣的:(在 SwiftUI 中创建一个带有可点击文本的大段落)