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

Formatting elements of expanding lists.

Forums > SwiftUI

Hi. I found this info on Expanding Lists super helpful!

https://www.hackingwithswift.com/quick-start/swiftui/how-to-create-expanding-lists

But I need to assistance on how/if I can format certain elements of the list. Here's the code from the page above.

struct Bookmark: Identifiable {
    let id = UUID()
    let name: String
    let icon: String
    var items: [Bookmark]?

    // some example websites
    static let apple = Bookmark(name: "Apple", icon: "1.circle")
    static let bbc = Bookmark(name: "BBC", icon: "square.and.pencil")
    static let swift = Bookmark(name: "Swift", icon: "bolt.fill")
    static let twitter = Bookmark(name: "Twitter", icon: "mic")

    // some example groups
    static let example1 = Bookmark(name: "Favorites", icon: "star", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
    static let example2 = Bookmark(name: "Recent", icon: "timer", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
    static let example3 = Bookmark(name: "Recommended", icon: "hand.thumbsup", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
}

struct ContentView: View {
    let items: [Bookmark] = [.example1, .example2, .example3]

    var body: some View {
        List(items, children: \.items) { row in
            HStack {
                Image(systemName: row.icon)
                Text(row.name)
            }
        }
    }
}

Here's what I would like to do

  1. I would like to 'star' icon and the 'favorites' to be of a specified color.
  2. I would like the list elements, Apple, BBC etc.... to be of a specified color (which may be different to 1 above).

Any advice, is this possible?

Thank you for any advice!!

edit: I might also want the 'favorites' to use a different font to the items in the list. :)

1      

@flaneur creates a Bookmark struct then asks:

I would like the list elements, Apple, BBC etc.... to be of a specified color
Thank you for any advice!!

I think you are saying you have an incomplete definition?

Why not add a color variable to your struct?

struct Bookmark: Identifiable {
    let id =         UUID()
    let name:        String
    let icon:        String
    var items:       [Bookmark]?
    var markColor =  Color.indigo // <-- sensible default value

    // Give each bookmark a color
    static let apple = Bookmark(name: "Apple", icon: "1.circle", markColor: .blue.opacity(0.4) ) 

Then when you display your Bookmark in a List view, you can specify the foreground, or background color using the Bookmark's markColor variable.

// Display a bookmark
Text("\(aBookmark.name) ).foregroundColor( aBookmark.markColor )

1      

Thank you, that worked a treat :)

if I may ask a final related question?

Here is an example of of of the element of the list

    static let visits = Bookmark(name: "Visits:", currency: "", kpiValue: "\(AnalyticsData.Visits[0])", icon: "figure.walk.motion")

The variable AnalyticsData.Visits is an integer. And I want to format it i.e. 123456999 = 123,456,999. How can I define the number formatting? I tired to used a number formatter in the definition of 'visits' but that didn't work.

1      

@flaneur has a number of questions. One is about numbers:

I want to format an integer 123456999 to appear as 123,456,999

One thing to remember about number formatting. You are NOT formatting a number. Instead think in a way such that you are creating a string version of your integer, with a user defined format.

Your request is probaly the most common, so I think it's a default.

Did you try this in Playgrounds?

// Run this line in Playgrounds. 
1232343450.formatted() // <-- No other magic required
// For me this returns 1,232,232,450    // <-- This is what you require?

let myAnalysisValue = 12345
print( myAnalysisValue.formatted() )  // <-- Give this a go!

1      

huh, I understand. Thank you will try this!

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.