SwiftUI 带下划线的超链接

问题描述 投票:0回答:1

我在 SwiftUI 中使用突出显示的超链接,但我也希望它们也带有下划线。这可能吗?

Text("This [link](https://google.com) goes to google")

如何让链接部分加下划线?

swift swiftui hyperlink localizedstringkey
1个回答
0
投票

您可以在属性字符串中搜索链接运行,并根据其范围修改主字符串的属性:

let baseString = "Hello [World](https://www.google.com) this is [Google](https://www.yahoo.com)"
var text = try! AttributedString(markdown: baseString)
let runsWithLinks = text.runs.filter { run in
    run.link != nil
}

for run in runsWithLinks {
    text[run.range].font = .system(size: 18).monospaced() // extra example
    text[run.range].foregroundColor = .gray               // extra example
    text[run.range].underlineStyle = .init(pattern: .solid, color: .green)
}

预览上面的示例会产生以下结果:

在我的测试过程中,使用

.underlineColor
没有效果。我不知道为什么。

© www.soinside.com 2019 - 2024. All rights reserved.