SwiftUI 链接长文本对齐多行

mgy*_*yky 2 text-alignment swiftui swiftui-link

问题

我正在使用 SwiftUI 的链接从应用程序中打开 Safari。但我有一个很长的链接文本。

目前,文本的第二行始终保持居中对齐。

我想要的是

我希望能够使用领先的 TextAlignment

所以我尝试使用multilineTextAlignment但没有成功。

代码

Link("Some long text even very looong even that long text here!", destination: URL(string: "https://www.apple.com/")!)
.multilineTextAlignment(.leading)
Run Code Online (Sandbox Code Playgroud)

需要帮忙。

mgy*_*yky 6

解决方案

我的解决方案是使用 Link 本身的另一个签名与multilineTextAlignment

Link(destination: URL(string: "https://www.apple.com/")!) {
    Text("Some long text even very looong even that long text here!")
    .multilineTextAlignment(.leading)
} 
Run Code Online (Sandbox Code Playgroud)

来自苹果文档

public struct Link<Label> : View where Label : View {

/// Creates a control, consisting of a URL and a label, used to navigate
/// to the given URL.
///
/// - Parameters:
///     - destination: The URL for the link.
///     - label: A view that describes the destination of URL.
public init(destination: URL, @ViewBuilder label: () -> Label)
Run Code Online (Sandbox Code Playgroud)

希望能帮助别人!

最好的