灵活的标签尺寸根据其文字长度 [英] flexible label size according to its text length

查看:111
本文介绍了灵活的标签尺寸根据其文字长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人身体



我怎样才能使标签的大小变得灵活....我的意思是当我增加它的文字长度...它没有显示所有内容,或更改字体....



i尝试自动调整但没有用



希望找到帮助



thnx

解决方案

你可以使用

 TextRenderer.MeasureText方法

赞...

字体字体= 字体(  Arial 14 ,FontStyle 。定期)​​; 
string a = 您需要的一些文字显示;
Size size = TextRenderer.MeasureText(a,font);
label1.Size = size;
label1.Text = a;





尺寸大小= TextRenderer.MeasureText(a,font); 

以像素为单位获取文本的大小。更改标签的大小,

 label1.Size = size; 

,并打印文本。


始终使用百分比(如size =100%)值而不是使用固定像素值

我们需要在C#中覆盖OnTextInput作为标签。当我们向标签添加一些文本时,将调用此方法。我们可以通过计算输入文本的宽度和高度来改变标签的大小....



使用(Graphics g = CreateGraphics())

{

SizeF size = g.MeasureString(text,lbl.Font,495);

lbl.Height =(int)Math.Ceiling(size .Height);

lbl.Text = text;

}



我们也可以使用TextRenderer.MeasureText ()方法获取输入文本的大小。


hi every body

how can i make th size of a label flexible ....i mean when i increase its text length ... it doesnt show all of its contents ,, or changing font ....

i tried autosize but no use

hope find a help

thnx

解决方案

You can use the

TextRenderer.MeasureText Method

Like...

Font font = new Font("Arial", 14, FontStyle.Regular);
string a = "some text you need to display";
Size size = TextRenderer.MeasureText(a, font);
label1.Size = size;
label1.Text = a;



The

Size size = TextRenderer.MeasureText(a, font);

gets the size of the text in pixel. Change the size of your label,

label1.Size = size;

, and print the text.


always use percentage (like size="100%") value instead of using fixed pixel value


We need to override OnTextInput for a label in C#. when we add some text to a label,this method will be called. we can change the size of the label by calculating its width and height of the input text....

using(Graphics g = CreateGraphics())
{
SizeF size = g.MeasureString(text, lbl.Font, 495);
lbl.Height = (int) Math.Ceiling(size.Height);
lbl.Text = text;
}

we can also use TextRenderer.MeasureText() method to get size of the input text.


这篇关于灵活的标签尺寸根据其文字长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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