如何在SwiftUI中为链接加下划线?

问题描述

这是我的链接

Link("Terms + Conditions",destination: URL(string: "https://my.app/terms_and_conditions.html")!)

我知道Text()一个underline()修饰符,但是Link()似乎没有一个修饰符。

有什么主意吗?

解决方法

链接具有一个带有标签参数的初始化程序: init(destination: URL,label: () -> Label)

因此,在您的示例中,您需要将Text("Terms + Conditions")视图设置为标签,并在文本视图上使用.underline()修饰符以获得预期的结果。

以Apples网站为例:

Link(destination: URL(string: "https://www.apple.com")!,label: {
    Text("Apple")
        .underline()
})