---
title: "Text and formatting"
canonical_url: "https://www.nutrient.io/guides/document-authoring/editing-content/text-and-formatting/"
md_url: "https://www.nutrient.io/guides/document-authoring/editing-content/text-and-formatting.md"
last_updated: "2026-07-08T00:00:00.000Z"
description: "Apply fonts, sizes, colors, highlights, bold, italic, and underline to text in Document Authoring with `TextView.setFormatting`."
---

# Format text

Text in a document lives in a paragraph’s text view. Inside a [transaction](https://www.nutrient.io/guides/document-authoring/editing-content/programmatic-editing.md), use [`setFormatting()`](https://www.nutrient.io/api/document-authoring/types/programmatic/textview/#setformatting) to apply character formatting to a whole paragraph or a specific range.

Character formatting includes font, size, color, highlight, bold, italic, and underline.

Before you start, make sure the Document Authoring library is installed and running in your app. If you haven’t set it up yet, refer to the [getting started](https://www.nutrient.io/sdk/document-authoring/getting-started.md) guide.

The examples omit error handling. Add it before you use this code in production.

## Formatting properties

[`setFormatting()`](https://www.nutrient.io/api/document-authoring/types/programmatic/textview/#setformatting) accepts a partial [`Formatting`](https://www.nutrient.io/api/document-authoring/types/programmatic/formatting/) object. Every field is optional, so set only the fields you want to change:

- `font` — Font family name.

- `fontSize` — Size in points.

- `bold`, `italic`, `underline`, `strikeout` — Boolean values.

- `color` — Text color as a hex string.

- `highlight` — Highlight color as a hex string.

## Format a whole paragraph

Call [`setFormatting()`](https://www.nutrient.io/api/document-authoring/types/programmatic/textview/#setformatting) without a range to format every character in the text view:

```js

const currentDoc = editor.currentDocument();

await currentDoc.transaction(async ({ draft }) => {
  const paragraph = draft.body().content().addParagraph();
  const textView = paragraph.asTextView();

  textView.setText('Quarterly results');
  textView.setFormatting({ bold: true, fontSize: 14, color: '#1D4ED8' });

  return true;
});

```

## Format part of a paragraph

Pass a [`Range`](https://www.nutrient.io/api/document-authoring/types/programmatic/range/) to format part of a paragraph. Get a range from [`setText()`](https://www.nutrient.io/api/document-authoring/types/programmatic/textview/#settext), which returns the range of the text it wrote, or from [`searchText()`](https://www.nutrient.io/api/document-authoring/types/programmatic/textview/#searchtext):

```js

await currentDoc.transaction(async ({ draft }) => {
  const paragraph = draft.body().content().addParagraph();
  const textView = paragraph.asTextView();

  textView.setText('This wording is final and approved.');

  const match = textView.searchText('final and approved');

  if (match) {
    textView.setFormatting({ italic: true, highlight: '#FEF08A' }, match.range);

  }

  return true;
});

```

## Add and format text

[`setText()`](https://www.nutrient.io/api/document-authoring/types/programmatic/textview/#settext) returns the range it wrote, so you can format text after adding it:

```js

await currentDoc.transaction(async ({ draft }) => {
  const textView = draft.body().content().addParagraph().asTextView();

  const range = textView.setText('Heads up');
  textView.setFormatting({ bold: true, color: '#B91C1C' }, range);

  return true;
});

```

To append text within the same paragraph instead of replacing it, use [`addInlineText()`](https://www.nutrient.io/api/document-authoring/types/programmatic/textview/#addinlinetext), which also returns a range you can format.

## Read existing formatting

A text view contains inline segments. Each segment has uniform formatting. Use [`inlines()`](https://www.nutrient.io/api/document-authoring/types/programmatic/textview/#inlines) to read those segments. This example finds bold segments:

```js

await currentDoc.transaction(async ({ draft }) => {
  for (const blockLevel of draft.body().content().blocklevels()) {
    if (blockLevel.type!== 'paragraph') continue;

    for (const { inline } of blockLevel.asTextView().inlines()) {
      if (inline.type!== 'text') continue;

      if (inline.formatting().bold) {
        console.log(`Bold: ${inline.plainText()}`);
      }
    }
  }

  return false;
});

```

## Set paragraph styles and properties

Use [`setFormatting()`](https://www.nutrient.io/api/document-authoring/types/programmatic/textview/#setformatting) for character-level formatting. For paragraph-level styling, use [`setParagraphProperties()`](https://www.nutrient.io/api/document-authoring/types/programmatic/paragraph/#setparagraphproperties) to set built-in styles, alignment, spacing, indentation, tab stops, borders, and shading:

```js

const currentDoc = editor.currentDocument();

await currentDoc.transaction(async ({ draft }) => {
  const paragraph = draft.body().content().addParagraph();

  paragraph.asTextView().setText('Styled paragraph');
  paragraph.setParagraphProperties({
    styleId: 'Heading 2',
    alignment: 'center',
    spaceBefore: 12,
    spaceAfter: 6,
    indentation: {
      left: 24,
      right: 24,
      firstLineShift: null,
    },
    shading: {
      fill: '#F3F4F6',

    },
  });

  return true;
});

```

Pass `{ clear: true }` as the second argument to clear existing direct paragraph properties before applying new ones:

```js

paragraph.setParagraphProperties(
  {
    alignment: 'left',
    styleId: 'Normal',
  },
  { clear: true },
);

```

For bullet and numbered lists, refer to the [lists](https://www.nutrient.io/guides/document-authoring/editing-content/lists.md) guide.

## Learn more

**[Find and replace](https://www.nutrient.io/guides/document-authoring/editing-content/find-and-replace.md)**\
Reformat matched text in bulk while replacing it.

**[Programmatic editing](https://www.nutrient.io/guides/document-authoring/editing-content/programmatic-editing.md)**\
Learn how transactions work and how to use paragraph properties and the document model.
---

## Related pages

- [Use copy/paste and HTML interoperability](/guides/document-authoring/editing-content/copy-paste-and-html-interoperability.md)
- [Work with lists](/guides/document-authoring/editing-content/lists.md)
- [Find and replace text](/guides/document-authoring/editing-content/find-and-replace.md)
- [Work with footnotes and endnotes](/guides/document-authoring/editing-content/footnotes-and-endnotes.md)
- [Edit documents programmatically](/guides/document-authoring/editing-content/programmatic-editing.md)
- [Work with images and shapes](/guides/document-authoring/editing-content/images-and-shapes.md)
- [Work with tables](/guides/document-authoring/editing-content/tables.md)

