monospaced()
Returns a fixed-width font from the same family as the base font.
Declaration
func monospaced() -> FontReturn Value
A fixed-width font from the same family as the base font, if one is available, and a default fixed-width font otherwise.
Discussion
If there’s no suitable font face in the same family, SwiftUI returns a default fixed-width font.
The following example adds the monospaced() modifier to the default system font, then applies this font to a Text view:
struct ContentView: View {
let myFont = Font
.system(size: 24)
.monospaced()
var body: some View {
Text("Hello, world!")
.font(myFont)
.padding()
.navigationTitle("Monospaced")
}
}[Image]
SwiftUI may provide different fixed-width replacements for standard user interface fonts (such as title, or a system font created with system(_:design:)) than for those same fonts when created by name with custom(_:size:).
The font(_:) modifier applies the font to all text within the view. To mix fixed-width text with other styles in the same Text view, use the init(_:) initializer to use an appropropriately-styled AttributedString for the text view’s content. You can use the init(markdown:options:baseURL:) initializer to provide a Markdown-formatted string containing the backtick-syntax (`…`) to apply code voice to specific ranges of the attributed string.