SwiftUI & FormatStyle

This is part of the FormatStyle Deep Dive series

The Text View struct now can accept any data type and a formatted: FormatStyle parameter. The view will then apply that formatter onto that data type and render the string on screen.


Download the Xcode Playground with all examples

See the examples as a gist


Never write Text("\()") again. Just pass in the right FormatStyle.

struct ContentView: View {
    static let twosdayDateComponents = DateComponents(
        year: 2022,
        month: 2,
        day: 22,
        hour: 2,
        minute: 22,
        second: 22,
        nanosecond: 22
    )

    var twosday: Date {
        Calendar(identifier: .gregorian).date(from: ContentView.twosdayDateComponents)!
    }

    var body: some View {
        VStack {
            Text(twosday, format: Date.FormatStyle(date: .complete, time: .complete))
            Text(twosday, format: .dateTime.hour())
            Text(twosday, format: .dateTime.year().month().day())
        }
        .padding()
    }
}

Will show:

Every data type and style is supported. Check out the full deep dive!


Attributed Strings

Text views can accept AttributedStrings as a parameter in their initializers. Most of the FormatStyle implementations provided by apple have the ability to output AttributedString values instead of plain String types.

The AttributedStrings Deep Dive has full details on this.


Download the Xcode Playground with all examples

See the examples as a gist


Tags: