Font

The Font dataclass configures typography for text views. It supports system semantic fonts, custom font sizes, custom font families, and font file loading.

import nib

# System semantic font
title = nib.Text("Welcome", font=nib.Font.TITLE)

# Custom size with weight
label = nib.Text("Label", font=nib.Font.system(14, nib.FontWeight.MEDIUM))

# Rounded design of the system font
round = nib.Text("Round", font=nib.Font.system(16, design=nib.FontDesign.ROUNDED))

# Custom font family from file
code = nib.Text("code", font=nib.Font.custom("Iosevka", 13, path="fonts/Iosevka.ttf"))

font

import nib


def main(app: nib.App):
    app.title = "Fonts"
    app.icon = nib.SFSymbol("textformat.size")
    app.width = 320

    app.build(
        nib.VStack(
            controls=[
                nib.Text("Large Title", font=nib.Font.LARGE_TITLE),
                nib.Text("Title", font=nib.Font.TITLE),
                nib.Text("Headline", font=nib.Font.HEADLINE),
                nib.Text("Body", font=nib.Font.BODY),
                nib.Text("Footnote", font=nib.Font.FOOTNOTE),
                nib.Divider(),
                nib.Text(
                    "system(18, LIGHT)", font=nib.Font.system(18, nib.FontWeight.LIGHT)
                ),
                nib.Text(
                    "system(18, BOLD)", font=nib.Font.system(18, nib.FontWeight.BOLD)
                ),
                nib.Text(
                    "system(18, ROUNDED)",
                    font=nib.Font.system(18, design=nib.FontDesign.ROUNDED),
                ),
                nib.Text(
                    "system(18, SERIF)",
                    font=nib.Font.system(18, design=nib.FontDesign.SERIF),
                ),
                nib.Text("custom Menlo 15", font=nib.Font.custom("Menlo", size=15)),
                nib.Text("custom Georgia 17", font=nib.Font.custom("Georgia", size=17)),
            ],
            spacing=6,
            padding=20,
            alignment=nib.HorizontalAlignment.LEADING,
        )
    )


nib.run(main)

System Fonts

System fonts use Apple's semantic sizing, which adapts to accessibility and platform settings.

Constant Description
Font.LARGE_TITLE Large title text (largest semantic size)
Font.TITLE Title text
Font.TITLE2 Secondary title text
Font.TITLE3 Tertiary title text
Font.HEADLINE Headline text (semibold by default)
Font.SUBHEADLINE Subheadline text
Font.BODY Body text (default reading size)
Font.CALLOUT Callout text (slightly smaller than body)
Font.FOOTNOTE Footnote text
Font.CAPTION Caption text
Font.CAPTION2 Secondary caption text (smallest semantic size)
nib.Text("Title", font=nib.Font.TITLE)
nib.Text("Body text", font=nib.Font.BODY)
nib.Text("Fine print", font=nib.Font.CAPTION)

Factory Methods

Font.system(size, weight=None, design=None)

Create a system font with a specific point size, and an optional weight and design.

font = nib.Font.system(18)
font = nib.Font.system(16, nib.FontWeight.BOLD)
font = nib.Font.system(14, "semibold")
font = nib.Font.system(16, design=nib.FontDesign.ROUNDED)
Parameter Type Default Description
size float none Font size in points.
weight FontWeight | str None Optional font weight.
design FontDesign | str None Optional design: rounded, serif or monospaced. See FontDesign.

Font.custom(name, size, weight=None, path=None)

Create a custom font by family name. Optionally provide a path to a .ttf or .otf file for runtime font loading.

# System-installed font
font = nib.Font.custom("Helvetica Neue", 16)

# Font from a bundled file
font = nib.Font.custom("Inter", 14, weight=nib.FontWeight.MEDIUM, path="fonts/Inter.ttf")
Parameter Type Default Description
name str none Font family name (e.g., "Inter", "Roboto").
size float none Font size in points.
weight FontWeight | str None Optional font weight.
path str None Path to .ttf or .otf file for runtime loading.

FontWeight

The FontWeight enum specifies text thickness. It can be used with Font.system(), Font.custom(), or the font_weight view modifier parameter.

Value SwiftUI Equivalent Description
FontWeight.ULTRA_LIGHT .ultraLight Thinnest weight
FontWeight.THIN .thin Very thin
FontWeight.LIGHT .light Light weight
FontWeight.REGULAR .regular Standard weight (default)
FontWeight.MEDIUM .medium Medium weight
FontWeight.SEMIBOLD .semibold Semi-bold weight
FontWeight.BOLD .bold Bold weight
FontWeight.HEAVY .heavy Heavy weight
FontWeight.BLACK .black Heaviest weight
nib.Text("Light", font_weight=nib.FontWeight.LIGHT)
nib.Text("Bold", font_weight=nib.FontWeight.BOLD)
nib.Text("Heavy", font=nib.Font.system(20, nib.FontWeight.HEAVY))

FontDesign

The FontDesign enum picks a design of the system font. Pass it to Font.system(), or build a preset with it: nib.Font(name="title", design=nib.FontDesign.ROUNDED) keeps the title size and weight. A custom family ignores the design.

Value SwiftUI Equivalent Description
FontDesign.DEFAULT .default San Francisco
FontDesign.ROUNDED .rounded SF Rounded
FontDesign.SERIF .serif New York
FontDesign.MONOSPACED .monospaced SF Mono
nib.Text("Round", font=nib.Font.system(16, design=nib.FontDesign.ROUNDED))
nib.Text("Serif title", font=nib.Font(name="title", design=nib.FontDesign.SERIF))
nib.Text("Code", font=nib.Font.system(13, design="monospaced"))

String Shortcut

When a string is passed to the font parameter, it is treated as a font family name.

# These are equivalent:
nib.Text("Hello", font=nib.Font.custom("Helvetica Neue", 16))
nib.Text("Hello", font="Helvetica Neue")

Note that the string shortcut does not allow specifying a size. The system default size is used. Use Font.custom() when you need to control the size.

Examples

Combining font and weight

import nib

def main(app: nib.App):
    app.build(
        nib.VStack(
            controls=[
                nib.Text("Large Title", font=nib.Font.LARGE_TITLE),
                nib.Text("Bold Headline", font=nib.Font.HEADLINE, font_weight=nib.FontWeight.BOLD),
                nib.Text("Custom Size", font=nib.Font.system(22, nib.FontWeight.SEMIBOLD)),
                nib.Text("Caption", font=nib.Font.CAPTION, foreground_color=nib.Color.SECONDARY),
            ],
            spacing=8,
            padding=16,
        )
    )

nib.run(main)

Loading a custom font file

import nib

mono_font = nib.Font.custom("JetBrains Mono", 13, path="fonts/JetBrainsMono-Regular.ttf")

def main(app: nib.App):
    app.build(
        nib.Text("fn main() { println!(\"Hello\"); }", font=mono_font)
    )

nib.run(main)