Contents

init(_:format:)

Creates a text view that displays the formatted representation of a nonstring type supported by a corresponding format style.

Declaration

init<F>(_ input: F.FormatInput, format: F) where F : FormatStyle, F.FormatInput : Equatable, F.FormatOutput == AttributedString

Parameters

  • input:

    The underlying value to display.

  • format:

    A format style of type F to convert the underlying value of type F.FormatInput to an attributed string representation.

Discussion

Use this initializer to create a text view backed by a nonstring value, using a FormatStyle to convert the type to an attributed string representation. Any changes to the value update the string displayed by the text view.

In the following example, three Text views present a date with different combinations of date and time fields, by using different Date.FormatStyle options.

@State private var myDate = Date()
var body: some View {
    VStack {
        Text(myDate, format: Date.FormatStyle(date: .numeric, time: .omitted).attributedStyle)
        Text(myDate, format: Date.FormatStyle(date: .complete, time: .complete).attributedStyle)
        Text(myDate, format: Date.FormatStyle().hour(.defaultDigitsNoAMPM).minute().attributedStyle)
    }
}

[Image]

See Also

Creating a text view