brakence
brakence
CC#
Created by brakence on 5/21/2025 in #help
How would I make automatic font size scaling with a custom control?
Hi, I'm new to WPF. I've been trying to make an application for the past few days and it's been going quite well so far. I took to StackOverflow to find code for a custom control for a TextBlock but with support for text stroke https://pastebin.com/CYQVxgpu However, I'm having an issue trying to make the font size automatically scale to fit the text content within bounds of the control. The text wraps, but I'm trying to make the font size decrease to fit all the text if it is too big, and to increase the font size if the text is too small In the UpdateFormattedText method, I've tried enumerating font sizes from biggest to smallest and finding the maximum font size that contains the text but that didn't work. An interesting behavior with this approach is that the font size is really small like it's at the minimum font size
double minFontSize = 4.0;
double maxFontSize = 32;

for (double size = maxFontSize; size >= minFontSize; size -= 0.5)
{
_FormattedText.SetFontSize(size);

if (_FormattedText.Height <= ActualHeight)
{
break;
}
}
double minFontSize = 4.0;
double maxFontSize = 32;

for (double size = maxFontSize; size >= minFontSize; size -= 0.5)
{
_FormattedText.SetFontSize(size);

if (_FormattedText.Height <= ActualHeight)
{
break;
}
}
Any help is appreciated
2 replies