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 == AttributedStringParameters
- input:
The underlying value to display.
- format:
A format style of type
Fto convert the underlying value of typeF.FormatInputto 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]