TextEditor

TextEditor control

A multi-line text editing view, similar to an HTML <textarea>. TextEditor is suited for editing longer content such as notes, descriptions, or code. The value property is reactive. Assigning a new value triggers an immediate UI update.

Constructor

nib.TextEditor(
    value="",
    placeholder=None,
    on_change=None,
    style=None,
    font=None,
    foreground_color=None,
    content_background=None,
    **modifiers,
)

Parameters

Parameter Type Default Description
value str "" Initial text.
placeholder str None Placeholder text displayed when the editor is empty (macOS 14+).
on_change Callable[[str], None] None Callback called when text changes. Receives the new text as a string argument.
style TextEditorStyle None Font, colors, line spacing, alignment and editor appearance in one object. See TextEditorStyle.
font Font None Text font. Overridden by style.font if both are provided.
foreground_color Color | str None Text color. Overridden by style.foreground_color if both are provided.
content_background Color | str | False None Editor background colour. False hides it. style takes precedence.
**modifiers Common view modifiers: padding, background, opacity, width, height, etc.

Mutable Properties

Property Type Description
value str Get or set the text. Setting triggers a UI update.
placeholder str Get or set the placeholder text. Setting triggers a UI update.
style TextEditorStyle Get or set the style configuration. Setting triggers a UI update.

Methods

Method Description
focus() Make the editor first responder.
blur() Resign first responder.
select_all() Select all text. The field must be focused.

TextEditorStyle

A dataclass with every TextEditor styling option. Pass it as style, or assign it to style later to restyle the editor. A new style rebuilds the editor and keeps its text. Fields set in the style win over the font, foreground_color and content_background parameters.

Constructor

nib.TextEditorStyle(
    font=None,
    foreground_color=None,
    background_color=None,
    line_spacing=None,
    text_alignment=None,
    line_limit=None,
    autocorrection_disabled=False,
    scrolls_disabled=False,
    editor_style=None,
)

Parameters

Parameter Type Default Description
font Font None Text font, such as Font.custom("Menlo", size=13) or a preset like Font.BODY.
foreground_color Color | str None Text color.
background_color Color | str None Editor background. Replaces the system one.
line_spacing float None Extra space between lines in points.
text_alignment Alignment | str None Alignment.LEADING, Alignment.CENTER or Alignment.TRAILING. See Alignment.
line_limit int None Maximum number of visible lines.
autocorrection_disabled bool False Turn off autocorrection while typing.
scrolls_disabled bool False Keep the editor from scrolling; it grows with its content instead.
editor_style EditorStyle | str None EditorStyle.PLAIN drops the border and background (macOS 14+). See EditorStyle.

Styled editor

texteditor_style

import nib


def main(app: nib.App):
    app.title = "Editor style"
    app.icon = nib.SFSymbol("chevron.left.forwardslash.chevron.right")
    app.width = 340
    app.height = 260

    editor = nib.TextEditor(
        value='def greet(name):\n    return f"Hello, {name}"\n\n\nprint(greet("nib"))',
        style=nib.TextEditorStyle(
            font=nib.Font.custom("Menlo", size=13),
            foreground_color=nib.Color(hex="#D4D4D4"),
            background_color=nib.Color(hex="#1E1E1E"),
            line_spacing=4,
            autocorrection_disabled=True,
            editor_style=nib.EditorStyle.PLAIN,
        ),
        width=300,
        height=180,
        corner_radius=8,
    )

    app.build(
        nib.VStack(
            controls=[nib.Text("snippet.py", font=nib.Font.HEADLINE), editor],
            spacing=8,
            padding=20,
            alignment=nib.HorizontalAlignment.LEADING,
        )
    )


nib.run(main)

Examples

Simple note editor

texteditor_simple_note_editor

import nib


def main(app: nib.App):
    app.title = "Note"
    app.icon = nib.SFSymbol("note.text")
    app.width = 340
    app.height = 240

    app.build(
        nib.VStack(
            controls=[
                nib.Text("Quick note", font=nib.Font.HEADLINE),
                nib.TextEditor(
                    value="Buy coffee beans\nCall the dentist\nShip nib 1.0",
                    width=300,
                    height=160,
                ),
            ],
            spacing=8,
            padding=20,
            alignment=nib.HorizontalAlignment.LEADING,
        )
    )


nib.run(main)

Editor with change tracking

texteditor_editor_change_tracking

import nib


def main(app: nib.App):
    app.title = "Draft"
    app.icon = nib.SFSymbol("pencil.line")
    app.width = 340
    app.height = 200

    draft = "Nib renders your interface with SwiftUI through a small Swift runtime."
    counter = nib.Text("", font=nib.Font.CAPTION, foreground_color=nib.Color.SECONDARY)

    def update(text):
        counter.content = f"{len(text.split())} words, {len(text)} characters"

    update(draft)

    app.build(
        nib.VStack(
            controls=[
                nib.TextEditor(value=draft, on_change=update, width=300, height=120),
                counter,
            ],
            spacing=6,
            padding=20,
            alignment=nib.HorizontalAlignment.TRAILING,
        )
    )


nib.run(main)