Contents

init(_:format:)

Creates a text view that displays the current system time as defined by the given format style, keeping the text up to date as time progresses.

Declaration

init<V, F>(_ source: TimeDataSource<V>, format: F) where V == F.FormatInput, F : DiscreteFormatStyle, F.FormatOutput == String

Discussion

Use this initializer to create a text view that updates as time progresses, just like init(_:style:), but with the flexibility of Foundation’s FormatStyle protocol.

In the following example, the first Text view presents the current date and time, whereas the second view displays a soccer clock counting from startDate. Both views are kept up to date as time progresses.

Text(.currentDate, format: .dateTime)
Text(.durationOffset(to: startDate), format: .time(pattern: .minuteSecond))

Redaction for Reduced Luminance

When the text is displayed with reduced luminance and frame rate, it automatically modifies the format or its output so it never shows outdated information.

If the format is known to SwiftUI and allows removing units or fields, SwiftUI removes parts that change more frequently than the frame rate allows. E.g. a string like 13 minutes, 22 seconds would change to just 13 minutes.

Otherwise, all digits in the formatted output are redacted using dashes.