Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center align text horizontally?

Tags:

imgui

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?

like image 454
lt123 Avatar asked Nov 02 '20 21:11

lt123


People also ask

How do I align horizontally and center in Word?

To align text horizontally on a page, highlight the text you want to center. Next, click the “Center Alignment” icon in the “Paragraph” group of the “Home” tab. Alternatively, you can use the Ctrl+E keyboard shortcut. Your text will now be horizontally aligned.

How do you center text vertically and horizontally?

1 Select the text you want to center between the top and bottom margins. 2 On the Page Layout tab, click the Page Setup Dialog Box Launcher. 3 Select the Layout tab. 4 In the Vertical alignment box, click Center 5 In the Apply to box, click Selected text, and then click OK.

How to center align text in a Div using CSS?

A horizontally aligned text with the CSS justify-content property. In the next example, we align the text of the <div> to the center with the CSS text-align property set to its "center" value. Also, we specify padding-top.

What is horizontal alignment in Microsoft Word?

Horizontal alignment, also known as centered alignment, positions the text evenly between the margins on either side of the page. This tool also allows you to be selective with the text you align horizontally, giving you more control over what you can do to your document. To align text horizontally on a page, highlight the text you want to center.

How to center text vertically and horizontally in HTML?

I am vertically centered. To center both vertically and horizontally, use padding and text-align: center: I am vertically and horizontally centered. Another trick is to use the line-height property with a value that is equal to the height property:

How do I center elements horizontally?

Here are some common elements you may want to center horizontally and different ways to do it. To center text or links horizontally, just use the text-align property with the value center: Use the shorthand margin property with the value 0 auto to center block-level elements like a div horizontally:


3 Answers

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());
}
like image 117
Yanis.F Avatar answered Oct 16 '22 13:10

Yanis.F


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();
}
like image 6
neo-mashiro Avatar answered Oct 16 '22 14:10

neo-mashiro


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());
}
like image 2
schnaader Avatar answered Oct 16 '22 14:10

schnaader