none
如何让Label自适应高度? RRS feed

  • 问题

  • 由于要显示的文本长度不固定,需要Label控件在文本内容较多的情况下自动扩展高度来显示所有文本,有什么快捷的方法实现这个功能吗?

    2008年7月8日 9:15

答案

  • you mean an auto-size lable? here is a sample (reference to Alex Yakhnin

    Code Snippet

    using System;
    using System.Drawing;
    using System.Windows.Forms;

    namespace MyControls
    {
    /// <summary>
    /// A label that supports vertical auto resizing.
    /// </summary>
    public class Label : System.Windows.Forms.Label
    {
    private bool bChanging = false;

    private SizeF MeasureStringExtended(Graphics g, string text, Font font, int desWidth)
    {
    string tempString = text;
    string workString = "";
    string outputstring = "";
    int npos = 1;
    int sp_pos = 0;
    int sp_pos1 = 0;
    int sp_pos2 = 0;
    int sp_pos3 = 0;
    bool bNeeded = false;
    int line = 0;
    int nWidth = 0;

    //get original size
    SizeF size = g.MeasureString(text, font);

    if (size.Width > desWidth)
    {
    while(tempString.Length > 0)
    {
    //Check for the last lane
    if (npos > tempString.Length)
    {
    outputstring = outputstring + tempString;
    line++;
    break;
    }
    workString = tempString.Substring(0, npos);
    //get the current width
    nWidth = (int)g.MeasureString(workString, font).Width;
    //check if we've got out of the destWidth
    if (nWidth > desWidth)
    {
    //try to find a space

    sp_pos1 = workString.LastIndexOf(" ");
    sp_pos2 = workString.LastIndexOf(";");
    sp_pos3 = workString.IndexOf("\r\n");
    if (sp_pos3 > 0)
    {
    sp_pos = sp_pos3;
    bNeeded = false;
    }
    else
    {
    if ((sp_pos2 > 0) && (sp_pos2 > sp_pos1))
    {
    sp_pos = sp_pos2;
    bNeeded = true;
    }
    else if (sp_pos1 > 0)
    {
    sp_pos = sp_pos1;
    bNeeded = true;
    }
    else
    {
    sp_pos = 0;
    bNeeded = true;
    }
    }

    if (sp_pos > 0)
    {
    //cut out the wrap lane we've found
    outputstring = outputstring + tempString.Substring(0,sp_pos + 1);
    if (bNeeded) outputstring = outputstring + "\r\n";
    tempString = tempString.Substring((sp_pos + 1), tempString.Length - (sp_pos + 1));
    line++;
    npos = 0;
    }
    else //no space
    {
    outputstring = outputstring + tempString.Substring(0,npos + 1);
    if (bNeeded) outputstring = outputstring + "\r\n";
    tempString = tempString.Substring(npos, tempString.Length - npos);
    line++;
    npos = 0;
    }
    }
    npos++;
    }
    }
    else
    {
    outputstring = text;
    }

    SizeF returnSize = g.MeasureString(outputstring, font);
    bChanging = true;
    this.Text = outputstring;
    bChanging = false;
    return returnSize;
    }

    private void InitializeComponent()
    {
    bChanging = false;
    }

    protected override void OnTextChanged(EventArgs e)
    {
    if(!bChanging)
    {
    Bitmap bmp = new Bitmap(175,175);
    Graphics graphics = Graphics.FromImage(bmp);
    SizeF size = new SizeF();

    size = MeasureStringExtended(graphics,this.Text,this.Font,175);
    this.Height = (int)size.Height;
    base.OnTextChanged (e);
    }
    }
    }
    }

     

     

     

    Good luck!

     

    Freesc Huang

    2008年7月17日 14:58
    版主
  • 我写了一个函数来实现,你可去下面地址看看是不是合适。

     

    http://www.winbile.net/bbs/blogs/tiger/archive/2008/7/30/73.aspx
    2008年7月30日 0:22

全部回复

  • you mean an auto-size lable? here is a sample (reference to Alex Yakhnin

    Code Snippet

    using System;
    using System.Drawing;
    using System.Windows.Forms;

    namespace MyControls
    {
    /// <summary>
    /// A label that supports vertical auto resizing.
    /// </summary>
    public class Label : System.Windows.Forms.Label
    {
    private bool bChanging = false;

    private SizeF MeasureStringExtended(Graphics g, string text, Font font, int desWidth)
    {
    string tempString = text;
    string workString = "";
    string outputstring = "";
    int npos = 1;
    int sp_pos = 0;
    int sp_pos1 = 0;
    int sp_pos2 = 0;
    int sp_pos3 = 0;
    bool bNeeded = false;
    int line = 0;
    int nWidth = 0;

    //get original size
    SizeF size = g.MeasureString(text, font);

    if (size.Width > desWidth)
    {
    while(tempString.Length > 0)
    {
    //Check for the last lane
    if (npos > tempString.Length)
    {
    outputstring = outputstring + tempString;
    line++;
    break;
    }
    workString = tempString.Substring(0, npos);
    //get the current width
    nWidth = (int)g.MeasureString(workString, font).Width;
    //check if we've got out of the destWidth
    if (nWidth > desWidth)
    {
    //try to find a space

    sp_pos1 = workString.LastIndexOf(" ");
    sp_pos2 = workString.LastIndexOf(";");
    sp_pos3 = workString.IndexOf("\r\n");
    if (sp_pos3 > 0)
    {
    sp_pos = sp_pos3;
    bNeeded = false;
    }
    else
    {
    if ((sp_pos2 > 0) && (sp_pos2 > sp_pos1))
    {
    sp_pos = sp_pos2;
    bNeeded = true;
    }
    else if (sp_pos1 > 0)
    {
    sp_pos = sp_pos1;
    bNeeded = true;
    }
    else
    {
    sp_pos = 0;
    bNeeded = true;
    }
    }

    if (sp_pos > 0)
    {
    //cut out the wrap lane we've found
    outputstring = outputstring + tempString.Substring(0,sp_pos + 1);
    if (bNeeded) outputstring = outputstring + "\r\n";
    tempString = tempString.Substring((sp_pos + 1), tempString.Length - (sp_pos + 1));
    line++;
    npos = 0;
    }
    else //no space
    {
    outputstring = outputstring + tempString.Substring(0,npos + 1);
    if (bNeeded) outputstring = outputstring + "\r\n";
    tempString = tempString.Substring(npos, tempString.Length - npos);
    line++;
    npos = 0;
    }
    }
    npos++;
    }
    }
    else
    {
    outputstring = text;
    }

    SizeF returnSize = g.MeasureString(outputstring, font);
    bChanging = true;
    this.Text = outputstring;
    bChanging = false;
    return returnSize;
    }

    private void InitializeComponent()
    {
    bChanging = false;
    }

    protected override void OnTextChanged(EventArgs e)
    {
    if(!bChanging)
    {
    Bitmap bmp = new Bitmap(175,175);
    Graphics graphics = Graphics.FromImage(bmp);
    SizeF size = new SizeF();

    size = MeasureStringExtended(graphics,this.Text,this.Font,175);
    this.Height = (int)size.Height;
    base.OnTextChanged (e);
    }
    }
    }
    }

     

     

     

    Good luck!

     

    Freesc Huang

    2008年7月17日 14:58
    版主
  •  

    在.net CF中,你可以继承Label控件,根据Text内容,重画Label控件。
    2008年7月23日 4:50
  • 我写了一个函数来实现,你可去下面地址看看是不是合适。

     

    http://www.winbile.net/bbs/blogs/tiger/archive/2008/7/30/73.aspx
    2008年7月30日 0:22