在mobile下面,经常要显示一些信息,由于是手持设备,一般需要显示的大些才好看见。为此,我写了一个自适应大小的函数,修改Label的字体,实现信息的最大显示。
函数使用方法:
Value 要显示的内容
LabShow 要显示到的Label类。
字体最小为8
void setTitleShow(string value, Label labShow)
{
SizeF MyFontSize = SizeF.Empty;
Font MyFont = null;
string strTest;
Bitmap bmp = new Bitmap(50, 50);
Graphics g = Graphics.FromImage(bmp);
strTest = value;
Rectangle MyRect = new Rectangle(0, 0, labShow.Width, labShow.Height);
MyFont = new Font("Tahoma", MyRect.Height, FontStyle.Regular);
MyFontSize = g.MeasureString(strTest, MyFont);
while ((MyFontSize.Width > MyRect.Width || MyFontSize.Height > MyRect.Height) && (MyFont.Size > 8))
{
MyFont = new Font("Tahoma", (MyFont.Size - .01F), FontStyle.Regular);
MyFontSize = g.MeasureString(strTest, MyFont);
}
labShow.Font = MyFont;
labShow.Text = value;
}
函数使用方法:
Value 要显示的内容
LabShow 要显示到的Label类。
字体最小为8
void setTitleShow(string value, Label labShow)
{
SizeF MyFontSize = SizeF.Empty;
Font MyFont = null;
string strTest;
Bitmap bmp = new Bitmap(50, 50);
Graphics g = Graphics.FromImage(bmp);
strTest = value;
Rectangle MyRect = new Rectangle(0, 0, labShow.Width, labShow.Height);
MyFont = new Font("Tahoma", MyRect.Height, FontStyle.Regular);
MyFontSize = g.MeasureString(strTest, MyFont);
while ((MyFontSize.Width > MyRect.Width || MyFontSize.Height > MyRect.Height) && (MyFont.Size > 8))
{
MyFont = new Font("Tahoma", (MyFont.Size - .01F), FontStyle.Regular);
MyFontSize = g.MeasureString(strTest, MyFont);
}
labShow.Font = MyFont;
labShow.Text = value;
}