开发者

Is there a way to estimate label width dynamically based on number of chars?

I have a column of labels that I'd like to make as wide as the widest text. If I'm using the "Small" text size in my webpage, is it possible to estimate the width of the label based on the largest text string available?

This is for ASP.net wi开发者_运维技巧th c#.

Thanks!


Render the HTML into a fake dummy and hidden div, somewhere off-page, then use Javascript/jQuery to measure the size of the div, which grows with the text. Don't forget to apply css white-space: nowrap; so it grows its width forever.


Starting with this: http://www.davidthielen.info/programming/2005/10/getting_accurat.html

I was able to tweak it to get the following code which works pretty good for me:

private static float MAX_RECT = 100000;
    private static float BITMAP_DPI = 2400;
    private static float ADJUST_TO_POINTS = 72.0f / BITMAP_DPI;

    public static float GetTextWidth(String fontname, float fontsize, FontStyle style, String text)
    {
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(1, 1);
        bmp.SetResolution(BITMAP_DPI, BITMAP_DPI);
        System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bmp);
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

        System.Drawing.StringFormat fmt = System.Drawing.StringFormat.GenericTypographic;
        fmt.Trimming=System.Drawing.StringTrimming.None;
        fmt.FormatFlags=System.Drawing.StringFormatFlags.MeasureTrailingSpaces | System.Drawing.StringFormatFlags.NoWrap;

        System.Drawing.Font font = new System.Drawing.Font(fontname, fontsize, style, System.Drawing.GraphicsUnit.Point);

        System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0, MAX_RECT, MAX_RECT);
        fmt.SetMeasurableCharacterRanges(new System.Drawing.CharacterRange[] { new System.Drawing.CharacterRange(0, text.Length) });

        System.Drawing.Region[] rgns = graphics.MeasureCharacterRanges(text, font, rect, fmt);
        rect = rgns[0].GetBounds(graphics);
        return rect.Width * ADJUST_TO_POINTS;
    }

    private static float GetLabelWidth(RS2005.ReportParameter[] parameters, String fontname, double fontSizeUnitVal, FontStyle style, String text)
    {
        float emSize = Convert.ToSingle(fontSizeUnitVal + 1);
        emSize = (emSize == 0 ? 12 : emSize);

        int pCnt = parameters.Count();
        string txt;
        float size = 0;
            for (int i = 0; i < pCnt; i++)
            {
                txt = parameters[i].Prompt == null ? parameters[i].Prompt : parameters[i].Name;
                var width=GetTextWidth(fontname, emSize, style, txt);

                if (width > size)
                    size = width;                                        
            }

            return size;            
    }

I call it like this:

size = GetLabelWidth(parameters, pPromptLbl.Font.Name, pPromptLbl.Font.Size.Unit.Value, FontStyle.Bold, pPromptLbl.Text);

                pPromptLbl.Width = Unit.Parse(Math.Round(size+30, 0).ToString());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜