当文本长于标签大小时调整标签的文本大小? [英] Resize text size of a label when the text gets longer than the label size?

查看:21
本文介绍了当文本长于标签大小时调整标签的文本大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示文件名的标签.. 我必须将标签的 AutoSize 设置为 False 以进行设计.
所以当文件名文本长于标签大小时..它像图片中那样被剪掉了.

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 than label size.. it got cut like in the picture.

label1.Size = new Size(200, 32);
label1.AutoSize = false;

当文本长于标签大小时,如何自动调整文本大小以适应标签大小?

How do I re-size the text automatically to fit the label size, when the text is longer than the label size?

推荐答案

您可以使用我下面的代码片段.系统需要一些循环来根据文本大小计算标签的字体.

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);
}

这篇关于当文本长于标签大小时调整标签的文本大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆