0

I'm posting this question because I'm in front of some kind of wall there.. I need to be able to condense the text of a label to make it fit but without touching at the font size. Basically, just squeeze the text.

Here an image that should be clearer to understand :

Normal/Condensed Font

enter image description here

I've searched a bit but it's pretty uncommon actually. I doubt it's possible without modifying the font itself but if anyone has an idea, that would be awesome.

N-B: I'm using VS2015.

MatSnow
  • 7,357
  • 3
  • 19
  • 31
Rice.P
  • 1
  • 1
  • For a WPF application there seems to be some flexibility via FontStreches, if the font supports it, and also some other workarounds [1]. But for a Forms Application I fear you have to use an already *condensed version* of your font as the label font. [1]: https://stackoverflow.com/questions/2948290/why-fontstretch-does-not-work-in-wpf – UniversE Feb 15 '18 at 10:58
  • That's what I was thinking, I should have gone for a WPF app instead of a WinForm from the beginning. I'll try to convert the regular version into a condensed one. Thanks anyway – Rice.P Feb 15 '18 at 11:12
  • I would think that you could draw the text onto a `Bitmap` first, then draw that `Bitmap` onto another `Bitmap` with a smaller `Width`. Because it's being treated as an image rather than text, the output may not be very pretty but it should work in principle. – jmcilhinney Feb 15 '18 at 12:42
  • 1
    You can use the overload of e.Graphics.MeasureString() that takes only the PointF origin of the string (so you measure the whole string). Let's call it `TheSizeF`. Calculate the scale factor (`_ScaleF = CSng(Label1.Width) / TheSizeF`) and then use `e.Graphics.ScaleTransform(_ScaleF, 1.0F)` before e.Graphics.DrawString() using the RectangleF overload (`New RectangleF(new Point(0,0), _ScaleF)`. This will scale the font to fit the label width. See also `TextRenderingHint`, can help smooth the results. – Jimi Feb 15 '18 at 14:12
  • Forgot to mention that `StringFormat` should be `StringFormatFlags.NoWrap` and the Scale is missing a piece: `(_ScaleF = CSng(Label1.Width) / TheSizeF.Width)` – Jimi Feb 15 '18 at 14:18

0 Answers0