UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Is there a way to set the alignment in a TableColumn in SwiftUI?

Forums > macOS

Hi. This seems odd. I've got a column in a Table that is going to be displaying currency amounts and I'd like it to be aligned to the trailing edge. Is there any way to do this in SwiftUI? I thought maybe there might be something when setting up TableColumn() but it seems to lack an alignment parameter. I thought maybe using something like .multilineTextAlignment(.trailing) might work below the Text() I'm putting into the cell, but it seems to be ignored.

3      

Just figured out that I could wrap my contents in an HStack and then use a Spacer() before my Text() to push my column content to the right side. In the absense of some other preferred way, this is at least working fine.

3      

The .multilineTextAlignment(.trailing) will align the text if it goes over a few lines to the trailing edge so if your Text is on one line you will not see anything with this modifier.

4      

@aone  

I've used the usual frame settings:

TableColumn("Name") { file in
  Text(file.fileSize)
    .frame(maxWidth: .infinity, alignment: .trailing)
}

4      

I'm working on a project in SwiftUI, and I'm facing a bit of a challenge with aligning currency amounts in a TableColumn. I want the amounts to be aligned to the trailing edge, giving the table a neat appearance. It seems like there's no direct way to set the alignment within the TableColumn initializer – there's no alignment parameter available. I tried using .multilineTextAlignment(.trailing) below the Text() view that holds the currency data in each cell, hoping it would work, but unfortunately, it's not having the desired effect.

3      

@Mathias seeks guidance, but provides written clues only:

I tried using .multilineTextAlignment(.trailing) below the Text()

First, welcome to HackingWithSwift forums. There are many here who have gone down the learning path and are willing to help answer questions, even providing code examples.

To help us help you, please provide some code snips of what you tried, and provide comments on what didn't work. Perhaps we can help you from there. Code snips have the added benefit of allowing others to learn from your coding techniques to incorporate into their solutions.

Keep coding!

3      

Replace the Text() view with:

HStack {
   Spacer()
   Text()
}

I suggest you read the Apple reference for TableColumn. It says the content: parameter should be a type that conforms to the View protocol. This means it can be a View composed of "child" views such as my example. This is a very common pattern in SwiftUI.

3      

@stpe  

Seems like we have a new solution to this, using TableColumnAlignment. However - currently in beta and only available on iOS 17.0+, macOS 14.0+, etc.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.