1. 如果只是添加颜色
只需要在PushStyleColor修改ImGuiCol_Text颜色
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 0.0f, 1.0f));
ImGui::Text("This is a text string");
ImGui::PopStyleColor(1);
2. 如果想添加颜色和文字大小
它提供了低级别的绘图函数,包括 AddText。然而,你不能直接通过 AddText 设置事件
ImGuiIO &io = ImGui::GetIO();
ImFont *font = gui.addFont(string("SourceHanSansCN-Normal.otf"), 13.0f, NULL, io.Fonts->GetGlyphRangesChineseFull(), true);
// 假设你有一个ImDrawList指针,名为drawList
ImDrawList *drawList = ImGui::GetForegroundDrawList();
// 使用AddText函数来添加文本
drawList->AddText(font, 28.8f, ImVec2(100, 100), IM_COL32_WHITE, "Hello, World!");
3. 如何添加自定义文字
ImFont *customFont = nullptr;
ImFont *customFont2 = nullptr;
Demo::setup(){
ofSetLogLevel(OF_LOG_VERBOSE);
// The call to setup() is required BEFORE adding fonts.
bool autoDraw = true;
ofxImGui::BaseTheme *theme = nullptr;
ImGuiConfigFlags customFlags = ImGuiConfigFlags_DockingEnable;
bool restoreGuiState = false;
bool showImGuiMouseCursor = false;
gui.setup(theme, autoDraw, customFlags, restoreGuiState, showImGuiMouseCursor);
// Add polish characters
static const ImWchar polishCharRanges[] =
{
0x0020,
0x00FF, // Basic Latin + Latin Supplement
0x0100,
0x01FF, // Polish characters
0,
};
static const ImWchar *normalCharRanges = ImGui::GetIO().Fonts->GetGlyphRangesDefault();
static const ImWchar *myCharRanges = polishCharRanges;
// myCharRanges = normalCharRanges; // Uncomment to disable polish characters
// Set font and keep a reference of it for using it later
customFont = gui.addFont("Roboto-Medium.ttf", 33.f, nullptr, myCharRanges);
customFont2 = gui.addFont("Roboto-Medium.ttf", 18.f, nullptr, myCharRanges);
}
Demo::draw(){
ImGui::PushFont(customFont);
ImGui::Text("this is test text");
ImGui::SameLine();
ImGui::PopFont();
ImGui::PushFont(customFont2);
ImGui::Text("this is test text");
ImGui::SameLine();
ImGui::PopFont();
ImGui::Text("this is test text");
}