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

Dan*_*Dan 6 string-concatenation swiftui

试图实现类似 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Zeo*_*ona 9

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

尝试这个,

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()}
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

  • 虽然这在这里确实有效,但本地化这将是一场噩梦,并且换行在小屏幕上不起作用。 (6认同)