winform 动态截断或者补全文字宽度

使用 TabControl 时,发现它的选项卡宽度会随文字长度变化,我自己做了一个浏览器,发现很难看,于是写了个算法,对文字长度进行填充或截断,效果很不错:

 

调用代码:
using (var g = tabs.CreateGraphics())
{
    tabPage.Text = "  " + PadAndEllipsis(g, tabs.Font, title, 150) + "  " ;
}

核心函数:

/// <summary>
/// 长度不够用空格前后填充,长度超过则截断并添加...
/// </summary>
/// <param name="g"></param>
/// <param name="font"></param>
/// <param name="str"></param>
/// <param name="maxWith"></param>
/// <returns></returns>
string PadAndEllipsis(Graphics g, Font font, string str, int maxWith)
{
    Func<string, float> getActualWidth = delegate (string text)
    {
        return g.MeasureString("|" + text + "|", font).Width - g.MeasureString("||", font).Width;
    };

    var originalWidth = getActualWidth(str);
    var expectedStr = str;

    //字符串增加空格使它突破宽度
    if (originalWidth < maxWith)
    {
        const string fillstr = " ";

        //快速填充使其超过标准宽度
        do
        {
            expectedStr = expectedStr + string.Concat(Enumerable.Repeat(fillstr, expectedStr.Length));
        } while (getActualWidth(expectedStr) < maxWith);

        //使用二分查找法
        var start = 0;
        var end = expectedStr.Length;
        while (start < end)
        {
            var pos = start + (int)Math.Ceiling(((double)end - start) / 2);
            if (getActualWidth(expectedStr.Substring(0, pos)) > maxWith)
            {
                if (end == pos)
                    break;
                end = pos;
            }
            else
            {
                if (start == pos)
                    break;
                start = pos;
            }
        }

        var padding = string.Concat(Enumerable.Repeat(fillstr, (end - str.Length) / 2));
        var result = padding + str + padding;
        var w = getActualWidth(result);
        return result;
    }
    else
    {
        const string suffixstr = "...";

        //使用二分查找法
        var start = 0;
        var end = expectedStr.Length;
        while (start < end)
        {
            var pos = start + (int)Math.Ceiling(((double)end - start) / 2);
            if (getActualWidth(expectedStr.Substring(0, pos) + suffixstr) > maxWith)
            {
                if (end == pos)
                    break;
                end = pos;
            }
            else
            {
                if (start == pos)
                    break;
                start = pos;
            }
        }

        var result = str.Substring(0, start) + suffixstr;
        return result;
    }
}

 

posted on 2024-07-16 13:34  空明流光  阅读(17)  评论(0编辑  收藏  举报

导航