Scale Label text in line with Label size

眉间皱痕 提交于 2020-01-06 04:53:08

问题


Looking for a way to resize a Labels text in line with the change in size of the Label. (ie. should a label increase in size by 50%, then the text should also increase in size by approx 50%).

There is plenty of articles around which adjust the text to fit fully within the size of a Label, which isn't suitable for what I want.
I already have a working version of this.

Somehow I need to be able to reference the previous size of a Label once the resize is complete in order to determine the ratio to use in order to resize the text.


回答1:


Perhaps what you could simply do is to get the actual size of the label with the contents inside then use a factor to obtain a new size.

You can get the render size of the label using;

System.Drawing.Size TextRenderer.MeasureText (string text, System.Drawing.Font font);

After obtaining the render size of the label you can re-set the label.width and label.height by multiplying the returned size with a factor (ex: Size *0.5) would give you the half of the required size.

You can use the same method in SizeChanged event handler of Label to determine if the MeasuredSize;

  • Less than current label size: increase font size until it reaches ~=labelsize
  • Larger than current label size: decrease font size until ~=labelsize or
  • Equal to current label size: do nothing?

I hope I could give you some insight on how to achieve this.




回答2:


An example using Graphics.ScaleTransform(), used to prepend a transformation matrix to the Graphics operations that follow.

Note that this transformation is applicable as long as the Graphics can draw a Font in a given size. If the Font is too small or too large, the rendering will not work as expected.
The minimum Font size should be 8.5 ~ 9 points and no more than 72.
Outside this range, the result are unpredictable. (e.g., the Text may simply disappear).
Thus, a Control minimum/maximum size should be set at design time and not be scaled beyond these measures.

The Label.TextAlign property is set to ContentAlignment.MiddleLeft here.
Also: .AutoSize = false; .Text = "Some text that needs to fit";

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

private void label1_Paint(object sender, PaintEventArgs e)
{
    Label label = sender as Label;
    e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
    float TextWidth = e.Graphics.MeasureString(label.Text, label.Font, label.Size, StringFormat.GenericTypographic).Width;
    float scale = (label.ClientSize.Width - label.Padding.Left) / TextWidth;
    e.Graphics.Clear(label.BackColor);
    e.Graphics.ScaleTransform(scale, scale);

    using (SolidBrush brush = new SolidBrush(label.ForeColor))
        e.Graphics.DrawString(label.Text, label.Font, brush,  
                              new RectangleF(PointF.Empty, label1.ClientSize), 
                              StringFormat.GenericTypographic);
}

The visual result:



来源:https://stackoverflow.com/questions/52987192/scale-label-text-in-line-with-label-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!