Use SwiftUI To Display The Current Time With TimelineView
Daily Coding Tip 058

This is the first Weekly Coding Tip, which are free for all subscribers and are posted every Monday.
In SwiftUI we can use TimelineView to refresh the content on a regular basis.
Before we do that, we’ll need a way of displaying the current time. Add a new Swift file called DigitalClockView, and add this view that takes a Date and gets the hours, minutes and seconds. The format is specified on the Text, ensuring that leading zeroes are shown such as “01” instead of 1.
The font has a maximum size of 1000, but it also has a minimum scale factor.
This allows the Text to hopefully be big enough on most screen sizes, but it shrinks down to work on smaller screen sizes too.
Now we need to use the previously mentioned TimelineView to display the current time.
I’ve also added a Toggle that allows you change whether seconds are displayed at runtime.



