Resize text size of a label when the text gets longer than the label size?

后端 未结 7 657
旧巷少年郎
旧巷少年郎 2020-12-05 07:47

I have a label that shows the file name .. I had to set AutoSize of the label to False for designing.
So when the file name text got longer tha

相关标签:
7条回答
  • 2020-12-05 08:10

    I think the easiest way could be to check the render size and if it is greater than the actual label size, decrease the fontsize of the label.

    private void label3_Paint(object sender, PaintEventArgs e) {

            Size sz = TextRenderer.MeasureText(label1.Text, label1.Font, label1.Size, TextFormatFlags.WordBreak);
    
            if (sz.Width > label1.Size.Width || sz.Height > label1.Size.Height)
            {
    
                DecreaseFontSize(label1);
    
            }
        }
    

    public void DecreaseFontSize(Label lbl) {

            lbl.Font = new System.Drawing.Font(lbl.Font.Name, lbl.Font.Size - 1, lbl.Font.Style);
    
        }
    
    0 讨论(0)
提交回复
热议问题