FastReport文本框根据高度缩小字体

我尝试使用报表自带的设置,AutoShrink = FontSize​​ 并设置 AutoShrinkMinSize=10避免字体过小。

但这种方法只会在文本行超出文本框宽度时才会触发字体缩小,无法满足在不触及宽度时,多次换行导致高度不够的情况下缩小字体

因此我使用以下代码进行后台判断行数缩小字体:

 1 private void Memo9_BeforePrint(object sender, EventArgs e)
 2     {
 3       //高度不够缩小字体
 4       TextObject textBox = sender as TextObject;  
 5       string text = textBox.Text;
 6       Font font = textBox.Font;
 7       float fontSize = font.Size;
 8       
 9       string REASON = Convert.ToString(Report.GetColumnValue("frxdsWF_PAY.REASON"));
10       int lineCount2 = 0;
11       using (StringReader reader = new StringReader(REASON))
12       {
13         while (reader.ReadLine() != null)
14         {
15           lineCount2++;
16         }
17       } 
18       if(lineCount2>11)
19       {
20         fontSize = 10;
21         textBox.Font = new Font(font.Name, fontSize, font.Style);
22       }
23       
24     }

此方法不足的是只判断了文本行数大于设定高度的文本框行数极限(11行)时进行字体缩小到指定大小,无法根据具体多少行缩小几个字号去动态设置字号

有更加好的想法的朋友可以在评论区分享,感谢!

posted @ 2025-10-13 10:53  官方小可爱  阅读(11)  评论(0)    收藏  举报