SwiftUI Text

At iOSDevUK in 2022, Natalia Panferova (Nil Coalesing) presented the talk Mysteries of SwiftUI Text View. The talk is available on YouTube.

It is a great talk that covers a lot of topics for SwiftUI Text views, including use of localised strings for rich text and markdown display as well as localisation, as well as other issues. Do watch the talk if you are using SwiftUI.

I was suprised to learn that there is a situation where the text could be automatically updated.The example was a date that could be represented as a timer. It shows a countdown to the date.

The code is shown below.

Text("Time until start: \(startDate, style: .timer)")
   .monospacedDigit()

This shows a message such as:

Time until start: 23:12:09.

The time will automatically update without any more coding.

The monospacedDigit() function modifies the text inside the Text view, changing any digits to use a monospaced font. Other text in the string is in whatever font is specified for the string.

The timer might be useful for a countdown to an event in the next day. If it is used for a date that is more than 24 hours away, the cumulative number of hours will be shown. For example, if an event is 29 days and 20 minutes away then the displayed date will be something like 629:20:00.

Do check out the talk - there is great insight into the use of what you might assume is a basic view in SwiftUI.