【问题标题】:How to change dynamically font size so the volume of text suites to rectangle size?如何动态更改字体大小以使文本套件的体积变为矩形大小?
【发布时间】:2021-09-13 20:34:54
【问题描述】:

我编写了一个应用程序,使我能够收集有关患者的病史。问题是我编写了一个代码,使我能够将文本放入矩形,但是当文本的体积大于宽度和高度时,它不适合矩形大小。

问题是当文本体积大于矩形大小时,如何动态地使字体变小?
代码:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    using (Font font1 = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point))
    {
        RectangleF rectF1 = new RectangleF(30, 30, 780, 200);
        e.Graphics.DrawString(richTextBox2.Text, font1, Brushes.Black, rectF1);
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1));
    }
}

【问题讨论】:

  • 为什么不把矩形变大或者加一个滚动条呢?让字体变小听起来会让人难以阅读。
  • 这能回答你的问题吗? Auto Resize Font to fit rectangle
  • 因为我想这样做使字体变小而且我想在将数据写入richtextbox并且滚动条不起作用后打印文档
  • 这不是我尝试过的答案,但代码使矩形适合字符串,我想要解决方法
  • 相反,代码计算字体大小以适应矩形。

标签: c# fonts


【解决方案1】:

正如我在 cmets 中提到的,一种选择是不断尝试越来越小的字体,直到找到合适的字体。这不是超级有效,但如果不经常这样做,它会起作用。

// Try to find a font size that fits
int GetFontSize(string text, Graphics graphics, RectangleF rect, string fontName, int maxFontSize=32)
{
    while (maxFontSize > 6)
    {
        using (var font = new Font(fontName, maxFontSize))
        {
            var calc = graphics.MeasureString(text, font, (int)rect.Width, StringFormat.GenericTypographic);
            if (calc.Height <= rect.Height)
            {
                break;
            }
        }
        maxFontSize -= 1;
    }
    return maxFontSize;
}

示例用法:

// Helper function to draw a string and rectangle
void DrawText(Graphics graphics, string text, RectangleF rect, string fontName="Arial")
{
    int fontSize = GetFontSize(text, graphics, rect, fontName);
    using(var brush = new SolidBrush(Color.Black))
    using(var pen = new Pen(brush))
    using (var font = new Font(fontName, fontSize))
    {
        graphics.DrawString(text, font, brush, rect, StringFormat.GenericTypographic);
        graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
    }
}

// Test cases
private void Form1_Paint(object sender, PaintEventArgs e)
{
    string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    RectangleF[] testCases = {
        new RectangleF(10, 10, 300, 100),
        new RectangleF(10, 120, 600, 200),
        new RectangleF(10, 330, 60, 400),
        new RectangleF(10, 330, 60, 400),
        new RectangleF(80, 330, 400, 60)
    };
    foreach (var r in testCases) {
        DrawText(e.Graphics, text, r);
    }
}

示例代码的输出:

【讨论】:

  • 这就是我一直在寻找的东西,非常感谢你!!!!
猜你喜欢
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 2011-06-19
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
相关资源
最近更新 更多