7

Just wondering if there is a way to apply a custom format for the DateStyle comp within SwiftUI's Text UI. For Example just the minutes or seconds like you would when using a DateFormatter. I want to display the dynamic dates for a Widget but it only offers a limited number of options. Why is this and has anyone found a way around this?

Apple talk more about Displaying Dynamic Dates here: https://developer.apple.com/documentation/widgetkit/keeping-a-widget-up-to-date

pawello2222
  • 46,897
  • 22
  • 145
  • 209
Russ
  • 71
  • 2
  • See [Updating time text label each minute in WidgetKit](https://stackoverflow.com/q/64053733/8697793) or [How to display Current Time (Realtime) in iOS 14 Home Widget](https://stackoverflow.com/q/64053270/8697793) – pawello2222 Sep 29 '20 at 14:52
  • AFAIK you can choose between the different styles: `time`, `date`, `relative`, `offset` and `timer`. But I don't think you can *customise* a style - you can try to mix them though (eg. by displaying two Texts next to each other). – pawello2222 Sep 29 '20 at 14:54
  • Yeah I know about the different styles. Pretty annoying that we can't customise it. I wonder what the reasoning is – Russ Sep 30 '20 at 09:04

1 Answers1

7

You won't like this answer, but as of iOS 14, the answer is there's no way to customize the relative style for Text. The relative style is new in iOS 14, and as always, Apple provide the bare minimum feature.

For now, you have only these choices:

  1. Use other Text.DateStyle, if they work for you
  2. Fallback to DateFormatter (and other formatters) with the tradeoff that widget can only refresh couple of times in a day

Hopefully this won't be the answer with iOS 15 or later.

samwize
  • 25,675
  • 15
  • 141
  • 186