Search code examples
swiftuitext-alignmentswiftui-link

SwiftUI Link Long Text Alignment Multiline


Problem

I'm using SwiftUI's Link to open Safari from the application. But I have a long text for the link.

For now, the second line of the text always keeps aligned at the center.

What I want

I want to be able to use leading TextAlignment with it.

So I've tried to use multilineTextAlignment but didn't work.

Code

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

Need help.


Solution

  • Solution

    My solution was using another signature of the Link itself with multilineTextAlignment.

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

    From Apple Documentation

    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)
    

    Hope will help someone else!

    Best