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

Token list/field view... or something like it?

Forums > SwiftUI

I know SwiftUI doesn't come with a TokenList built in (maybe some day?) So I'm trying to simulate one.

The situation: I've got a book with multiple authors (say, like an anthology.)

I've created a "quasi-token" view and put it in a horizontal list:

    private func personToken(_ person: NamedPerson) -> some View {
        Text(person.description)
            .font(.headline)
            .padding(8.0)
            .foregroundColor(.white)
            .background(Color.blue)
            .cornerRadius(25.0)
    }

    private var authorTokenList: some View {
        HStack {
            ForEach(book.authors) { author in
                personToken(author)
            }
        }
    }

Which looks pretty good at first glance:

image

My first problem (reserving the possibility to expand as further issues become apparent) is that the tokens themselves wrap if the HStack gets too wide for the view:

image

It would be better if the HStack resized vertically to accomodate more tokens, but while I can find tutorials on making a scrollable horizontal view, I'm not sure how to make a vertically-resizable HStack.

I'm also going to want to add context-menu like functionality to each token on a long press gesture (maybe?) to delete or re-order the tokens, but I need to try to work that out before I have any specific questions about it.

3      

To keep the tokens from wrapping you can apply a .lineLimit(1) modifier to the Text, but I'm not sure off the top of my head what you could use rather than an HStack for a vertically expanding container.

3      

For vertically resizing to accomodate more tokens, take a look at this: Flexible layouts in SwiftUI | FIVE STARS Might be useful.

3      

You could also look at if LazyVGrid, LazyHGrid, LazyHStack or LazyVStack (in combination any other ideas here) works for you.

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!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.