Instance Method インスタンスメソッド

monospacedDigit()

Returns a modified font that uses fixed-width digits, while leaving other characters proportionally spaced.

Declaration 宣言

func monospacedDigit() -> Font

Return Value 戻り値

A font that uses fixed-width numeric characters.

Discussion 議論

This modifier only affects numeric characters, and leaves all other characters unchanged. If the base font doesn’t support fixed-width, or monospace digits, the font remains unchanged.

The following example shows two text fields arranged in a VStack. Both text fields specify the 12-point system font, with the second adding the monospacedDigit() modifier to the font. Because the text includes the digit 1, normally a narrow character in proportional fonts, the second text field becomes wider than the first.


@State private var userText = "Effect of monospacing digits: 111,111."


var body: some View {
    VStack {
        TextField("Proportional", text: $userText)
            .font(.system(size: 12))
        TextField("Monospaced", text: $userText)
            .font(.system(size: 12).monospacedDigit())
    }
    .padding()
    .navigationTitle(Text("Font + monospacedDigit()"))
}

A macOS window showing two text fields arranged vertically. Each

See Also 参照

Styling a Font フォントのスタイリング