Initializer

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 == String

Parameters パラメータ

input

The underlying value to display.

format

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

Discussion 議論

Use this initializer to create a text view backed by a nonstring value, using a FormatStyle to convert the type to a 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))
        Text(myDate, format: Date.FormatStyle(date: .complete, time: .complete))
        Text(myDate, format: Date.FormatStyle().hour(.defaultDigitsNoAMPM).minute())
    }
}

Three vertically stacked text views showing the date with different

See Also 参照

Creating a Text View with Formatting