2013-10-28 38 views
2

我想在指定的矩形中绘制字符串,但问题是具有指定字体的字符串可能在矩形的边界之外。如何缩放字体以适合指定的矩形

所以我想要一种方法来缩放字符串字体以适应指定矩形区域中的字符串。

public Font scaleFont(String text, RectangleF rect, Graphics graphics, Font pFont) 
     { 
      float fontSize = 20.0f; 
      Font font = pFont; 
      float width = graphics.MeasureString(text, pFont).Width; 
      float height = graphics.MeasureString(text, pFont).Height; 
      fontSize = ((rect.Width/width) * pFont.Size); 
      float heig = (rect.Height/height); 
      return new Font(pFont.FontFamily, fontSize); 
     } 

上面的代码给我缩放字体,以适应在长方形的顶部区域的字符串,但我想,以适应整个区域(附加了上述算法中的认沽)

Output of algo

但我想要以下输出。

enter image description here

+0

换句话说,你想单词被包装的框。我理解你了吗? –

+0

@ AndreyAtapin,我已经更新了这个问题。 – user2528012

+0

你为什么要做'Font font = pFont',然后永远不要使用它? –

回答

1

通过在全区安装你是指?

enter image description here

如果是这样,常见的方式是借鉴了位图和装配一个你想要的任何对象文本。

为避免像素化,您可以使用自己的代码来检测最佳字体大小和最佳位图大小。

+0

我认为你在这里的目标是正确的。我正在做一个项目,我不得不做这种事情,这也是我提出的最好主意。我用这篇文章作为指导,对我来说效果很好。感谢Michael Kuehl--这里是链接[csharp-font-scaling](http://tech.pro/tutorial/691/csharp-tutorial-font-scaling) –