Imgui: FontSize for Childs

Created on 11 Feb 2017  路  4Comments  路  Source: ocornut/imgui

Dear Ocornut,

is there a chance to change the font size within a ImGui::Child or a ImGui::Begin?
I have checked a lot of options... no result so far.

I just want to change my normal FontSize between my Childs.
Thank you.

enhancement fontext

Most helpful comment

The scaled font is very blurry.
Whichever method use. ( PushFont/PopFont or SetWindowFontScale)
@ocornut

111

All 4 comments

Have a two fonts of different sizes and using ImGui::PushFont()/ImGui::PopFont()?

As a dubious workaround, you can get your current ImFont*, change the font->Scale value and then call PushFont() on this font. After you call PopFont you want to restore the size.

You can also use SetWindowFontScale() which is a stupidly old API (that will be removed by an eventual PushFontSize/PopFontSize()`.

I won't add the explicit API until font sizes are handled more correctly at the atlas rasterizing level.

The scaled font is very blurry.
Whichever method use. ( PushFont/PopFont or SetWindowFontScale)
@ocornut

111

I had a similar issue as I wanted each line of text to fill the width of its parent window. I've now sorted this and here is my process should it prove useful or of interest.

void Initialise()
{
ImFont *fontA = AddDefaultFont(13);
ImFont *fontB = AddDefaultFont(64);
ImFont *fontC = AddDefaultFont(256);
}

ImFont* ImGuiOverlay::AddDefaultFont( float pixel_size )
{
ImGuiIO &io = ImGui::GetIO();
ImFontConfig config;
config.SizePixels = pixel_size;
config.OversampleH = config.OversampleV = 1;
config.PixelSnapH = true;
ImFont *font = io.Fonts->AddFontDefault(&config);
return font;
}

void ImGuiOverlay::DoFitTextToWindow(ImFont *font, const char *text)
{
ImGui::PushFont( font );
ImVec2 sz = ImGui::CalcTextSize(text);
ImGui::PopFont();
float canvasWidth = ImGui::GetWindowContentRegionWidth();
float origScale = font->Scale;
font->Scale = canvasWidth / sz.x;
ImGui::PushFont( font );
ImGui::Text("%s", text);
ImGui::PopFont();
font->Scale = origScale;
}

void Draw()
{
// .... New frame and Window begin
DoFitTextToWindow( fontA, "Some Text" );
DoFitTextToWindow( fontB, "Some Other Text" );
DoFitTextToWindow( fontC, "Some Final Text" );
// .... Window end and end frame
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

noche-x picture noche-x  路  3Comments

ILoveImgui picture ILoveImgui  路  3Comments

NPatch picture NPatch  路  3Comments

the-lay picture the-lay  路  3Comments

mkanakis picture mkanakis  路  3Comments