10

I am creating a text in ImGui. It automatically aligns right, how do I make just that one text align in the center?

ImGui::Text("Example Text");

I don't believe there is a function to do this. I know you can do it for a box or widget, but how would I for a simple text?

lt123
  • 117
  • 1
  • 2
  • 8

6 Answers6

10
void TextCentered(std::string text) {
    auto windowWidth = ImGui::GetWindowSize().x;
    auto textWidth   = ImGui::CalcTextSize(text.c_str()).x;

    ImGui::SetCursorPosX((windowWidth - textWidth) * 0.5f);
    ImGui::Text(text.c_str());
}
Yanis.F
  • 612
  • 6
  • 18
6

Just want to add a solution for multi-line text to save 5 minutes for someone.

void TextCentered(std::string text) {
    float win_width = ImGui::GetWindowSize().x;
    float text_width = ImGui::CalcTextSize(text.c_str()).x;

    // calculate the indentation that centers the text on one line, relative
    // to window left, regardless of the `ImGuiStyleVar_WindowPadding` value
    float text_indentation = (win_width - text_width) * 0.5f;

    // if text is too long to be drawn on one line, `text_indentation` can
    // become too small or even negative, so we check a minimum indentation
    float min_indentation = 20.0f;
    if (text_indentation <= min_indentation) {
        text_indentation = min_indentation;
    }

    ImGui::SameLine(text_indentation);
    ImGui::PushTextWrapPos(win_width - text_indentation);
    ImGui::TextWrapped(text.c_str());
    ImGui::PopTextWrapPos();
}
neo-mashiro
  • 422
  • 4
  • 13
  • Hey cool function thx mate, but I do wonder, when I want to reposition the text-input with ur function with "ImGui.SetCursorPos(..)" it won't let me, it. just ignores it, I dunno understand why.. – Shpendicus Oct 24 '22 at 22:23
2

This comment on a similar GitHub issue could help, haven't tried it myself though:

void TextCenter(std::string text) {
    float font_size = ImGui::GetFontSize() * text.size() / 2;
    ImGui::SameLine(
        ImGui::GetWindowSize().x / 2 -
        font_size + (font_size / 2)
    );

    ImGui::Text(text.c_str());
}
schnaader
  • 49,103
  • 10
  • 104
  • 136
1

Although I've gone with the method others have listed here, there is another method using Selectable:

static bool test = false;
ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, ImVec2(0.5f, 0.5f));
ImGui::Selectable("Test", &test, 0, ImVec2(80,10));
ImGui::PopStyleVar();

The bad:

  • Items have hover effects that cannot be disabled without making text gray.
  • may have extra overhead

The Good:

  • Easy
  • NB: Unlike the other solutions, this one works in Table cells when table column auto-width is used. (double click on dividers)
Marius
  • 3,372
  • 1
  • 30
  • 36
0

Well more performant way without std::string

void TextCenter(const char* text, ...) {
    va_list vaList = nullptr;
    va_start(vaList, &text, );

    float font_size = ImGui::GetFontSize() * strlen(text) / 2;
    ImGui::SameLine(
        ImGui::GetWindowSize().x / 2 -
        font_size + (font_size / 2)
    );

    ImGui::TextV(text, vaList);

    va_end(vaList);
}
-1

In short:

void DrawTextCentered(const char* text)
{
    ImGui::SetCursorPosX( (ImGui::GetWindowWidth() - ImGui::CalcTextSize(text).x) / 2.f);
    ImGui::Text(text);
}
NullifiedVlad
  • 53
  • 1
  • 4
  • This solution seems to have already been provided in other answers, can you add more details to your answer to make it stand on its own? – Cristik Apr 20 '22 at 05:21