# TextBlocks

Use the TextBlock class to display text.

Setting the TextBlock.text property is the only way to show a text string. Because TextBlock inherits from GraphObject, some GraphObject properties will affect text. But there are additional text-only options regarding how that text is formatted and drawn.

In these simplistic demonstrations, the code programmatically creates a Part and adds it to the Diagram. Once you learn about models and data binding you will generally not create parts (nodes or links) programmatically.

## Font and colors

The size and stylistic appearance of the text is specified by the TextBlock.font. The value may be any CSS font specifier string.

The text is drawn using the TextBlock.stroke brush. The value may be any CSS color string or a Brush. By default the stroke is "black".

You can also specify the brush to use as the background: GraphObject.background. This defaults to no brush at all, which results in a transparent background. The background is always rectangular.

In these simplistic demonstrations, the code programmatically creates a Part and adds it to the Diagram. Once you learn about models and data binding you will generally not create parts (nodes or links) programmatically.

      diagram.add(
        new go.Part("Vertical")
          .add(
            new go.TextBlock({ text: "a Text Block" }),
            new go.TextBlock({ text: "a Text Block", stroke: "red" }),
            new go.TextBlock({ text: "a Text Block", background: "lightblue" }),
            new go.TextBlock({ text: "a Text Block", font: "bold 14pt serif" })
          ));

### Icon Fonts

In some cases, you can show an icon that is provided by a font, instead of using a Picture or a Shape. First, make sure the font is loaded in the page before creating the diagram. Then you can show a string containing the Unicode code point for the desired icon.

    // Explicitly load a font.  Only do this once, not each time you load a model.
    const awesome = new FontFace("awesome",
        "url(https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/fonts/fontawesome-webfont.ttf)");
    document.fonts.add(awesome);
    // Wait for the Font Awesome font to load before actually loading the diagram.
    awesome.load().then(() => load());

    function load() {
      diagram.add(
        new go.Node("Auto")
          .add(
            new go.Shape({ fill: "lightgreen" }),
            new go.Panel("Horizontal", { margin: 8 })
              .add(
                new go.TextBlock(
                  { text: "\uf030", font: "14pt awesome" }),
                new go.TextBlock("an example using FontAwesome",
                  { margin: new go.Margin(0, 0, 0, 2) })
              )
          ));
    }

## Natural Sizing of TextBlocks Varies by Browser

Because different browsers measure canvas text differently, TextBlocks are the only objects in GoJS that may have inconsistent natural sizes between browsers or different devices. For this reason, if you need objects to measure precisely and consistently across all browsers (such as for testing), TextBlocks without an explicit size (GraphObject.desiredSize or GraphObject.width and GraphObject.height) should not be used to dictate the size of any objects. For example, if you have a TextBlock with natural size inside an Panel.Auto Panel, set the size on that Auto Panel to fix the size of the area occupied by both the panel and its text.

## Sizing and Clipping

The natural size of a TextBlock is just big enough to render the text string with the given font. However the actual size of the TextBlock can be larger or smaller in either dimension. Larger dimensions result in areas with no text; smaller dimensions result in clipping.

To demonstrate this, the examples below start with a naturally sized TextBlock, followed by ones with decreasing explicit sizes. To better show the actual size of the TextBlocks below, we have given them lightgreen backgrounds.

      diagram.add(
        new go.Part("Vertical")
          .add(
            new go.TextBlock({ text: "a Text Block", background: "lightgreen", margin: 2 }),
            new go.TextBlock({ text: "a Text Block", background: "lightgreen", margin: 2,
                               width: 100, height: 33 }),
            new go.TextBlock({ text: "a Text Block", background: "lightgreen", margin: 2,
                               width: 60, height: 33 }),
            new go.TextBlock({ text: "a Text Block", background: "lightgreen", margin: 2,
                               width: 50, height: 22 }),
            new go.TextBlock({ text: "a Text Block", background: "lightgreen", margin: 2,
                               width: 40, height: 9 })
          ));

## Max Lines and Overflow

You can constrain the TextBlock's available size using GraphObject.desiredSize (width and height), but you can also limit the vertical height with TextBlock.maxLines, which will limit the number allowed. When there isn't enough space to display all text, you can decide how to use the remaining space with different values for TextBlock.overflow. There are additional options in the wrapping section below.

The example below starts with a naturally sized TextBlock, followed by ones with a max of 2 lines using the default TextBlock.overflow value of `OverflowClip`, followed by one using the TextBlock.overflow value of `OverflowEllipsis`.

      diagram.contentAlignment = go.Spot.Center,
      diagram.add(
        new go.Part("Vertical")
          .add(
            // Allow any number of lines, no clipping needed:
            new go.TextBlock({ text: "a Text Block that takes 4 lines",
                              font: '14pt sans-serif',
                              background: "lightblue",
                              overflow: go.TextOverflow.Clip /* the default value */,
                              // no maximum number of lines of text (the default)
                              margin: 2,
                              width: 90 }),
            // Allow only 2 lines, OverflowClip:
            new go.TextBlock({ text: "a Text Block that takes 4 lines",
                              font: '14pt sans-serif',
                              background: "lightblue",
                              overflow: go.TextOverflow.Clip /* the default value */,
                              maxLines: 2,
                              margin: 2,
                              width: 90 }),
            // Allow only 2 lines, OverflowEllipsis:
            new go.TextBlock({ text: "a Text Block that takes 4 lines",
                              font: '14pt sans-serif',
                              background: "lightblue",
                              overflow: go.TextOverflow.Ellipsis,
                              maxLines: 2,
                              margin: 2,
                              width: 90 })
          ));

## Wrapping

Text can also be automatically wrapped onto additional lines. In order for wrapping to happen, the TextBlock.wrap property must not be None, and there must be some constraint on the width to be narrower than it would naturally be.

In the following examples, the first TextBlock gets its natural size, the second is limited to 50 wide but is not allowed to wrap, and the other examples are limited to the same width but are allowed to wrap.

      diagram.add(
        new go.Part("Vertical")
          .add(
            new go.TextBlock({ text: "a Text Block", background: "lightgreen", margin: 2 }),
            new go.TextBlock({ text: "a Text Block", background: "lightgreen", margin: 2,
                              width: 50, wrap: go.Wrap.None }),
            new go.TextBlock({ text: "a Text Block", background: "lightgreen", margin: 2,
                              width: 50, wrap: go.Wrap.DesiredSize }),
            new go.TextBlock({ text: "a Text Block", background: "lightgreen", margin: 2,
                              width: 50, wrap: go.Wrap.Fit })
          ));

## Text Alignment

The TextBlock.textAlign property specifies where to draw the characters horizontally within the size of the TextBlock. The value must be a CSS string.

This is different than the GraphObject.alignment property, which controls where to place the object within the area allocated by the parent Panel.

      diagram.add(
        new go.Part("Horizontal")
          .add(
            new go.Panel("Vertical",
                { width: 150, defaultStretch: go.Stretch.Horizontal })
              .add(
                new go.TextBlock({ text: "textAlign: 'left'", background: "lightgreen", margin: 2,
                                  textAlign: "left" }),
                new go.TextBlock({ text: "textAlign: 'center'", background: "lightgreen", margin: 2,
                                  textAlign: "center" }),
                new go.TextBlock({ text: "textAlign: 'right'", background: "lightgreen", margin: 2,
                                  textAlign: "right" })
              ),
            new go.Panel("Vertical",
                { width: 150, defaultStretch: go.Stretch.None })
              .add(
                new go.TextBlock({ text: "alignment: Left", background: "lightgreen", margin: 2,
                                  alignment: go.Spot.Left }),
                new go.TextBlock({ text: "alignment: Center", background: "lightgreen", margin: 2,
                                  alignment: go.Spot.Center }),
                new go.TextBlock({ text: "alignment: Right", background: "lightgreen", margin: 2,
                                  alignment: go.Spot.Right })
              )
          ));

The TextBlock.verticalAlignment property controls the vertical alignment of the glyphs within the bounds. Neither TextBlock.textAlign nor TextBlock.verticalAlignment affect the sizing of the TextBlock.

      diagram.add(
        new go.Part("Horizontal")
          .add(
            new go.TextBlock({ text: "verticalAlignment: Top", verticalAlignment: go.Spot.Top,
                              width: 170, height: 60, background: "lightgreen", margin: 10 }),
            new go.TextBlock({ text: "verticalAlignment: Center", verticalAlignment: go.Spot.Center,
                              width: 170, height: 60, background: "lightgreen", margin: 10 }),
            new go.TextBlock({ text: "verticalAlignment: Bottom", verticalAlignment: go.Spot.Bottom,
                              width: 170, height: 60, background: "lightgreen", margin: 10 })
          ));

## TextAlign and Multiline or Wrapping

The TextBlock.textAlign property is useful even when the TextBlock has its natural size. This occurs when the text occupies multiple lines, whether by embedded newlines causing line breaks or by wrapping. You can control whether text starting with the first newline character is ignored by setting the TextBlock.isMultiline. By default both multiline and wrapping are enabled.

      diagram.add(
        new go.Part("Vertical")
          .add(
            new go.TextBlock({ text: "a Text Block\nwith three logical lines\nof text",
                              background: "lightgreen", margin: 2,
                              isMultiline: false }),
            new go.TextBlock({ text: "a Text Block\nwith three logical lines\nof text",
                              background: "lightgreen", margin: 2,
                              isMultiline: true }),
            new go.TextBlock({ text: "a Text Block\nwith three logical lines\nof centered text",
                              background: "lightgreen", margin: 2,
                              isMultiline: true, textAlign: "center" }),
            new go.TextBlock({ text: "a single line of centered text that should" +
                                    " wrap because we will limit the width",
                              background: "lightgreen", margin: 2, width: 80,
                              wrap: go.Wrap.Fit, textAlign: "center" })
          ));

## Indenting

Each line of text is normally trimmed of leading and trailing spaces before rendering. If you wish to indent a line of text by preserving leading spaces, start the line with the zero-width space character: `\u200B`. Wikipedia: Zero-width space

      diagram.add(
        new go.Part("Vertical")
          .add(
            new go.TextBlock({ text: "left aligned\n\u200B  indent two\n\u200B    indent four",
                              background: "lightgreen", margin: 2, width: 150 }),
            new go.TextBlock({ text: "\u200B  This is an indented paragraph consisting of lots of text that wraps naturally.",
                              background: "lightgreen", margin: 2, width: 150 })
          ));

You can also use a `\u200B` character if you want to preserve spaces at the end of a line.

## Flipping

You can flip text horizontally and vertically with the TextBlock.flip property:

      diagram.add(
        new go.Part("Table", {
            defaultColumnSeparatorStrokeWidth: 3,
            defaultColumnSeparatorStroke: "gray",
            defaultSeparatorPadding: 5
          })
          .add(
            new go.TextBlock({ text: "Hello", column: 0, margin: 2, font: '26px serif',
                            flip: go.Flip.None
                          }),
            new go.TextBlock("None (default)", { row: 1, column: 0 }),
            new go.TextBlock({ text: "Hello", column: 1, margin: 2, font: '26px serif',
                            flip: go.Flip.Horizontal
                          }),
            new go.TextBlock("FlipHorizontal", { row: 1, column: 1 }),
            new go.TextBlock({ text: "Hello", column: 2, margin: 2, font: '26px serif',
                            flip: go.Flip.Vertical
                          }),
            new go.TextBlock("FlipVertical", { row: 1, column: 2 }),
            new go.TextBlock({ text: "Hello", column: 3, margin: 2, font: '26px serif',
                            flip: go.Flip.Both
                          }),
            new go.TextBlock("FlipBoth", { row: 1, column: 3 })
          ));

## Editing

GoJS also supports the in-place editing of text by the user. You just need to set the TextBlock.editable property to true.

If you want to provide text validation of the user's input, you can set the TextBlock.textValidation property to a function. You can also provide a more customized or sophisticated text editor by setting the TextBlock.textEditor property. There is an example of text validation on the Validation intro page.

      diagram.add(
        new go.Part()
          .add(
            new go.TextBlock(
              { text: "select and then click to edit",
                background: "lightblue",
                editable: true, isMultiline: false })
          ));
      diagram.add(
        new go.Part()
          .add(
            new go.TextBlock(
              { text: "this one allows embedded newlines",
                background: "lightblue",
                editable: true })
          ));

Note how the TextBlock on the left only supports single line text, so the text editor accepts any edits and completes editing when the user hits the Enter key.