是否可以在 Text() 中添加内联按钮?

Is it possible to add an in-line button within a Text()?

试图实现类似 tinder 的条款和条件,但是我不知道如何使条款和隐私政策在 swift UI

中可点击

创建帐户或登录即表示您同意我们的条款隐私政策

var body: some View {
        Text(Constants.privacyText)
        .applyCustomLabelTextProperties(size: size, kerning: 2)
        .foregroundColor(color)
//        .lineLimit(2)
        +
        Text(" ")
        +
        Text(Constants.terms).underline()
            .foregroundColor(color)
//            .onTapGesture {
//                print("test")
//            }
        +
        Text(" and ")
            .foregroundColor(color)
        +
        Text(Constants.privacyPolicy).underline()
            .foregroundColor(color)
    }
}

尝试在 Text() 上应用可点击的 .onTapGesture 并尝试添加按钮并将其连接到文本中,但均未成功。

一个简单的方法:

 Text("By creating an account you agree to our ")
 Button(action: { self.openPrivacyPage()}){ Text("Privacy Policy")}
  .buttonStyle(BorderlessButtonStyle())

您可以使用字体 sizes/types 等进一步设置文本和按钮的样式。使用 HStack 保持在同一行。

这导致:

已经太晚了,但如果这对某人有帮助,我很高兴 :)

试试这个,

var body: some View {
        VStack {
            Text("By creating an account you agree to our ")
            HStack {
                Button(action: { }){ Text("Terms").underline() }
                Text(" and ")
                Button(action: { }){ Text("Privacy Policy").underline()}
            }
        }
    }

结果:

问题是当你有一个动态文本时,整个段落中间应该有一个可点击的图像或文本,而不是总是在同一个地方。 让我们假设您有一个动态导入的文本并放置在视图的某个位置。并且您想点亮一个感兴趣的单词并使其可点击以赋予意义或其他内容。我还没有找到在 SwiftUI 中执行此操作的方法。

let firstHalfOfParagraph = Text($importedStringBeforeWord)
let secondHalfOfParagraph = Text($importedStringAfterWord) 
let word = Text($importedWord)
return firstHalfOfParagraph + word.onTapGesture{print("action")} + secondHalfOfParagraph

这永远行不通,因为 word 现在是 AnyView 而不是 Text,而且它不能与 Text 连接!因此,进退两难。