iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📅

[SwiftUI] Displaying Text using FormatStyle

に公開

I looked into the new initializer for SwiftUI's Text available since iOS 15 and wanted to take some notes.

DateFormatter

Previously, when I wanted to display a Date in a Text view, I used a DateFormatter.
I would convert the Date to a String using a defined formatter.

let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .none

Text(dateFormatter.string(from: Date.now)) // 10/17/2023

However, I found a way to write it much more concisely.

Text(Date.now, format: Date.FormatStyle(date: .numeric, time: .omitted))
// 10/17/2023

By specifying a format, it is now possible to accept types other than String.

Differences Between Old and New Styles

The Style used previously was defined as an enum.

extension DateFormatter {

  public enum Style : UInt, @unchecked Sendable {

    case none = 0

    case short = 1

    case medium = 2

    case long = 3

    case full = 4
  }
}

On the other hand, the new Style is divided into DateStyle and TimeStyle, with static properties defined for each.
It is also apparent that subtle differences in behavior are expressed with more accurate names.

@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension Date.FormatStyle {

    /// Predefined date styles varied in lengths or the components included. The exact format depends on the locale.
    public struct DateStyle : Codable, Hashable, Sendable {

        /// Excludes the date part.
        public static let omitted: Date.FormatStyle.DateStyle

        /// Shows date components in their numeric form. For example, "10/21/2015".
        public static let numeric: Date.FormatStyle.DateStyle

        /// Shows date components in their abbreviated form if possible. For example, "Oct 21, 2015".
        public static let abbreviated: Date.FormatStyle.DateStyle

        /// Shows date components in their long form if possible. For example, "October 21, 2015".
        public static let long: Date.FormatStyle.DateStyle

        /// Shows the complete day. For example, "Wednesday, October 21, 2015".
        public static let complete: Date.FormatStyle.DateStyle

        ...
    }

    /// Predefined time styles varied in lengths or the components included. The exact format depends on the locale.
    public struct TimeStyle : Codable, Hashable, Sendable {

        /// Excludes the time part.
        public static let omitted: Date.FormatStyle.TimeStyle

        /// For example, `04:29 PM`, `16:29`.
        public static let shortened: Date.FormatStyle.TimeStyle

        /// For example, `4:29:24 PM`, `16:29:24`.
        public static let standard: Date.FormatStyle.TimeStyle

        /// For example, `4:29:24 PM PDT`, `16:29:24 GMT`.
        public static let complete: Date.FormatStyle.TimeStyle

        ...
    }
}

Other FormatStyles

It is now possible to express various types flexibly, not just Date. The following example uses IntegerFormatStyle to represent a price from an Int.

Text(123456, format: IntegerFormatStyle.Currency.currency(code: "JPY")) // ¥123,456
Text(123456, format: IntegerFormatStyle.Currency.currency(code: "USD")) // $123,456.00

Please check out this article for a clear list of styles.
https://qiita.com/hyuga_amazia/items/e69e5b4e8c65fcd013a9#integerformatstyle

Bonus

You can also simply return a String from a Date using the formatted method.

Text(Date.now.formatted(date: .numeric, time: .omitted))

I hope this is helpful to someone.
Thank you.

References

https://developer.apple.com/documentation/foundation/formatstyle
https://developer.apple.com/documentation/swiftui/text/init(_:format:)

Discussion