Would a countdown or timer widget be possible?

WidgetKit looks great, I love that it's SwiftUI and lightweight. I'm just trying to see if I understood everything correctly by using an example: Widgets are not mini apps, but really "freeze-dried" SwiftUI view hierarchies that are then "thawed out" by the system as needed.

That being the case, a developer could not build a "countdown" or "timer" widget that would show a label with the current, accurate-to-the-second value of the countdown or timer, correct?

While I could imagine that to be a real-world use-case, I do understand that the home screen should not have a lot of things animating and moving, but to be "calm".

Accepted Reply

You can use SwiftUI's new Text initializer to display the relative time to a certain timestamp. For more information you can checkout: https://developer.apple.com/documentation/swiftui/text/init(_:style:)

This Text view updates automatically without you needing to provide new entries for every second.

Replies

You can use SwiftUI's new Text initializer to display the relative time to a certain timestamp. For more information you can checkout: https://developer.apple.com/documentation/swiftui/text/init(_:style:)

This Text view updates automatically without you needing to provide new entries for every second.
Wow, that's great! Thank you for the quick reply :)
Any tips for adjusting this new text view from truncating text with ... Currently when using this, the label periodically will display ... as the label size adjusts.

Code Block
Text(Date().addingTimeInterval(0), style: .relative)
.font(Font.system(.body, design: .monospaced))



Hey Developer Tools Engineer,
Is this the only form of "animation" that is available in widgets (without providing new entries for every second) ?

Found the answer here:
https://developer.apple.com/documentation/widgetkit/keeping-a-widget-up-to-date
(Display Dynamic Dates)

its posible to add custom format to type .timer? example 00:13:26
After some trial and error, it seems that the widget will auto update the date displayed but no re-layout will be done. Causing text to be truncated with ... when the display date grows longer.

For example when using .relative style, display date could change from "1 sec" to "1 min 20 sec" which is much longer. If the original space doesn't fit "1 min 20 sec" then it may become "1 mi..." which looks quite bad.

Quick workaround is to try allocating more space to Text element in advance.