Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 481
mgyky Avatar asked Nov 03 '25 13:11

mgyky


1 Answers

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

like image 80
mgyky Avatar answered Nov 06 '25 04:11

mgyky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!