> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/graphics/text/flatredball-graphics-textmanager-getwidth.md).

# GetWidth

### Introduction

The GetWidth method returns the width of a piece of text. The GetWidth method returns the width of text, and can accept a variety of arguments. The overloads provide flexibility depending on how much information is needed.

### Overloads

```
public static float GetWidth(string text)

public static float GetWidth(string text, float spacing)

public static float GetWidth(string text, float spacing, BitmapFont font)

public static float GetWidth(string text, float spacing, BitmapFont font, int startIndex, int count)

public static float GetWidth(string text, float spacing, BitmapFont font, int startIndex, int count, List<float> widthList)

public static float GetWidth(Text text)
```

### Code Example

The following code creates a Text object and a Line which can be moved between letters with the left and right arrows on the keyboard:

Add the following using statements:

```
using FlatRedBall.Graphics;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Input;
```

Add the following to your Game or Screen's class scope:

```
Text mText;
int mIndex = 0;
Line mLine;
```

Add the following to your Game's Initialize or Screen's CustomInitialize:

```
mText = TextManager.AddText("Hello I am a string");
mLine = ShapeManager.AddLine();
mLine.SetFromAbsoluteEndpoints(
    new Vector3(0, 1, 0),
    new Vector3(0, -1, 0));
```

Add the following to your Game's Update or Screen's CustomActivity:

```
if (InputManager.Keyboard.KeyPushed(Keys.Right))
{
    mIndex++;
    if (mIndex > mText.DisplayText.Length)
    {
        mIndex--;
    }
    UpdateToIndex();
}
if (InputManager.Keyboard.KeyPushed(Keys.Left))
{
    mIndex--;
    if (mIndex < 0)
    {
        mIndex = 0;
    }
    UpdateToIndex();
}
```

Add the following function at class scope:

```
private void UpdateToIndex()
{
    float position = TextManager.GetWidth(
        mText.DisplayText, mText.Spacing, mText.Font, 0, mIndex);

    mLine.X = mText.X + position;
}
```

![TextManagerGetWidth.PNG](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-e1f16d385ee6a58bbd3f0a4ccd76a574eb5a4142%2Fmigrated_media-TextManagerGetWidth.PNG?alt=media)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/graphics/text/flatredball-graphics-textmanager-getwidth.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
