0

i have one screen on my app witch gets data from firebase and then displays it. But i want to have 2 Text() view the one below the other, but for some reason the text is not displaying in the left of the view

The problem:

My Code:

struct ResourceItem: View {
    
    var title: String
    var url: String
    
    var body: some View {
        VStack {
            
            Link(destination: URL(string: url)!) {
                
                Text(title)
                    .bold()
                    .font(.system(size: 18))
                
                Text(url)
                    .bold()
                    .foregroundColor(.gray)
                    
            }
        }
    }
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
GSepetadelis
  • 272
  • 9
  • 24
  • Does this answer your question? [SwiftUI text-alignment](https://stackoverflow.com/questions/56443535/swiftui-text-alignment) – Simon Aug 24 '21 at 13:22

3 Answers3

1

try this:

VStack (alignment: .leading) {
    Text(title)
        .bold()
        .font(.system(size: 18))
    Text(url)
        .bold()
        .foregroundColor(.gray)
}
1

Link creates it's own VStack with default configuration. That's why when you place Link inside VStack - outer container is ignored.

You need to place VStack(alignment: .leading) inside Link:

struct ResourceItem: View {
    
    var title: String
    var url: String
    
    var body: some View {
        Link(destination: URL(string: url)!) {
            VStack(alignment: .leading) {
                Text(title)
                    .bold()
                    .font(.system(size: 18))
                Text(url)
                    .bold()
                    .foregroundColor(.gray)
            }
        }
    }
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
-1

If you need this texts to be on one line - just put them In a horizontal stack -

HStack (alignment: .leading) {
Text(title)
    .bold()
    .font(.system(size: 18))
Text(url)
    .bold()
    .foregroundColor(.gray)

}

  • This isn't what they were looking for. They were talking about the alignment of the text. `"i want to have 2 Text() view the one below the other"` – George Aug 24 '21 at 13:04