has anyone dealt with adaptive fonts, according to the size of the text, which must fit in certain pixels and is willing to share a little experience .. Do you know any suitable library.
(These are labels for different sized printers)
I wrote a service for this and in certain cases .. Capital letters, specific characters, a single character at the end of the line has deviations and transfers it to a new line, eating one line of text .. I have attached the method that calculates it.
private bool ValidateFont(Graphics graphics, string text,Font font, SizeF sizeF, StringFormat format)
{
int lines;
int charsCount;
var labelLines = graphics.MeasureString(text, font, sizeF, format, out chars, out lines);
var currentRowCount = 0;
var extraRows = 0;
var rows = text.Split('
');
var rect = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
if (rows != null && rows.Length > 0)
{
for (int t = 0; t < rows.Length && rect.Y < rectangle.Height; t++)
{
if (!string.IsNullOrEmpty(rows[t]))
{
var rowHeight = font.GetHeight(graphics) * ((t + 1) + extraRows);
rect.Y = (int)rowHeight;
var textSize = graphics.MeasureString(rows[t], font, sizeF, format, out charsCount, out currentRowCount);
extraRows = extraRows + (currentRowCount - 1);
}
}
var fontHeight = font.GetHeight(graphics);
var measuredTextLines = extraRows + rows.Length;
var linesLimit = sizeF.Height / fontHeight;
return measuredTextLines <= linesLimit;
}
return false;
}
var textSize = graphics.MeasureString(rows[t], font, sizeF, format, out charsCount, out currentRowCount);
And the calculation itself returns a width smaller than that of the label, which is not the case
question from:
https://stackoverflow.com/questions/65940413/adaptive-font-calculation-system-drawing-c-net 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…