首页 > 解决方案 > 如何在 SwiftUI 中为链接添加下划线?

问题描述

这是我的链接:

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

我知道它Text()有一个underline()修饰符,但似乎没有一个用于Link().

任何想法?

标签: iosswiftswiftui

解决方案


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

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

以苹果网站为例:

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

推荐阅读