首页 > 解决方案 > 如何在 ImGui 中更改 InputText 的文本颜色?

问题描述

我正在寻找如何更改“名称”打印上显示的文本的颜色,但我对如何做到这一点一无所知。我想让它变成绿色,感谢帮助或提示:D

// Name
            ImGui::InputText("Name", selected_entry.name, 32);


标签: c++buttonimgui

解决方案


可以使用样式在全局范围内更改文本颜色

ImGuiStyle* style = &ImGui::GetStyle();
style->Colors[ImGuiCol_Text] = ImVec4(1.0f, 1.0f, 1.0f, 1.00f);

单个小部件的颜色可以通过推送/弹出样式更改

char txt_green[] = "text green";
char txt_def[] = "text default";

// Particular widget styling
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0,255,0,255));
ImGui::InputText("##text1", txt_green, sizeof(txt_green));
ImGui::PopStyleColor();

...

// Use global style colors
ImGui::InputText("##text2", txt_def, sizeof(txt_def));

输出:

彩色文本输入

同样,如果您想为输入文本和标签使用不同的颜色,我建议您轻松使用两个小部件。

char txt_def[] = "text default";

ImGui::InputText("##Name", txt_def, sizeof(txt_def));
ImGui::SameLine();
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0, 255, 0, 255));
ImGui::Text("Name");
ImGui::PopStyleColor();

输出:


推荐阅读