Text Alignment in SwiftUI

I've created an HStack and am trying to make one of those titles that you find in Apple apps (Messages, Mail, etc.) at the top left. I've gotten the text to the right size, made it bold, and aligned to the left and used .frame(alignment: .top) to align it to the top, but for some reason it stays in the middle. What am I doing wrong? Below is the code and an image of the issue.



import SwiftUI



struct HomeScreen: View {

    var body: some View {

        HStack{

            Text("Tasks").font(.largeTitle).fontWeight(.bold).frame(maxWidth: .infinity, alignment: .leading).padding()

                .frame(alignment: .top)

            

    }

        

}



struct HomeScreen_Previews: PreviewProvider {

    static var previews: some View {

        HomeScreen()

    }

}

}
![]("https://developer.apple.com/forums/content/attachment/9d21e948-cd73-404c-9b3c-02f7898c8cfe" "title=Screen Shot 2022-06-05 at 3.49.25 AM.png;width=704;height=1350")

Answered by robnotyou in 715741022

Use a VStack to position your content below your heading...
...then add a Spacer, to force everything to the top.

struct HomeScreen: View {
    
    var body: some View {
        VStack {
            HStack{
                Text("Tasks")
                    .font(.largeTitle)
                    .fontWeight(.bold)
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .padding()
            }
            
            // TODO: your content goes here...
            Text("content...")
            Spacer() /// forces content to top
        }
    }
}

Did you try align .topLeading ?

For some reason, screenshot does not show.

For some reason, screenshot doesn't show.

Accepted Answer

Use a VStack to position your content below your heading...
...then add a Spacer, to force everything to the top.

struct HomeScreen: View {
    
    var body: some View {
        VStack {
            HStack{
                Text("Tasks")
                    .font(.largeTitle)
                    .fontWeight(.bold)
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .padding()
            }
            
            // TODO: your content goes here...
            Text("content...")
            Spacer() /// forces content to top
        }
    }
}
Text Alignment in SwiftUI
 
 
Q