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

后端 未结 7 676
旧巷少年郎
旧巷少年郎 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:00

    You can use my code snippet below. System needs some loops to calculate the label's font based on text size.

    while(label1.Width < System.Windows.Forms.TextRenderer.MeasureText(label1.Text, 
         new Font(label1.Font.FontFamily, label1.Font.Size, label1.Font.Style)).Width)
    {
        label1.Font = new Font(label1.Font.FontFamily, label1.Font.Size - 0.5f, label1.Font.Style);
    }
    

提交回复
热议问题