我正在 ImGui 中创建文本。它会自动右对齐,如何使该文本居中对齐?
ImGui::Text("Example Text");
Run Code Online (Sandbox Code Playgroud)
我不相信有一个函数可以做到这一点。我知道你可以为一个盒子或小部件做到这一点,但是对于一个简单的文本我该怎么做呢?
Yan*_*s.F 11
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());
}
Run Code Online (Sandbox Code Playgroud)
只是想添加一个多行文本的解决方案,以节省某人的咖啡时间。
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();
}
Run Code Online (Sandbox Code Playgroud)
关于类似 GitHub 问题的评论可能会有所帮助,但我自己还没有尝试过:
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());
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22454 次 |
最近记录: |