六、ImGui如何给文字添加颜色和大小

本文介绍了如何在ImGui中修改文本颜色,调整字体大小,以及添加自定义字体,包括使用`ImGuiCol_Text`、`ImFont`和`AddText`函数,以及如何通过`addFont`加载并管理自定义字体如Roboto-Medium.ttf。
摘要由CSDN通过智能技术生成
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");
}

📎Roboto-Medium.ttf.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值