Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

asp.net - Adaptive font calculation System Drawing - C#/.NET

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...