c# label 自定义行间距

label 添加 Paint 事件。用"\n" 段落换行

     private void label2_Paint(object sender, PaintEventArgs e)
        {
            int LineDistance = 8;//行间距
            System.Windows.Forms.Label label = sender as System.Windows.Forms.Label;
            System.Drawing.Font drawFont = label.Font;
            label.AutoSize = false;
            SolidBrush drawBrush = new SolidBrush(label.ForeColor);
            Graphics g = e.Graphics;
            g.Clear(label.BackColor);
            StringFormat drawFormat = new StringFormat();    
            string[] arrDrawString = label.Text.Split(new char[] { '\n'});

            int height = 0;
            foreach (string str in arrDrawString)
            {
                //文本的矩形区域大小
                SizeF textSize = g.MeasureString(str, label.Font);

                //计算行数
                int strLineCount = Convert.ToInt32(Math.Ceiling(textSize.Width / label.Width));


                height += Convert.ToInt16((textSize.Height + LineDistance) * strLineCount);
            }

            label.Height = height;   //计算调整后的高度
            float netTextPos_Y = 0;   // 下一行的位置
            foreach (string drawString in arrDrawString)
            {
                bool drawText = false;
                int strLenght = 1;   // 长度
                int startIndex = 0;  // 开始位置
                for (int i = 0; i < drawString.Length; i++, strLenght++)
                {
                    string subN = drawString.Substring(startIndex, strLenght);
                    if (startIndex + strLenght >= drawString.Length)
                    {
                        drawText = true;
                    }
                    else
                    {
                        string subN1 = drawString.Substring(startIndex, strLenght + 1);
                        if (g.MeasureString(subN, label.Font).Width <= label.Width && g.MeasureString(subN1, label.Font).Width > label.Width)
                        {
                            drawText = true;
                        }
                    }
                    if (drawText)
                    {
                        drawText = false;
                        strLenght = 0;
                        startIndex = i + 1;
                        SizeF textSize = g.MeasureString(subN, label.Font);
                        e.Graphics.DrawString(subN, drawFont, drawBrush, 0, netTextPos_Y , drawFormat);
                        netTextPos_Y = netTextPos_Y + textSize.Height + LineDistance;
                    }
                }
            }
        }

效果图:

相关推荐
水煮庄周鱼鱼2 小时前
C# 入门简介
开发语言·c#
软件黑马王子3 小时前
Unity游戏制作中的C#基础(6)方法和类的知识点深度剖析
开发语言·游戏·unity·c#
Nicole Potter3 小时前
请说明C#中的List是如何扩容的?
开发语言·面试·c#
gu205 小时前
c#编程:学习Linq,重几个简单示例开始
开发语言·学习·c#·linq
pchmi9 小时前
CNN常用卷积核
深度学习·神经网络·机器学习·cnn·c#
yuanpan10 小时前
23种设计模式之《组合模式(Composite)》在c#中的应用及理解
开发语言·设计模式·c#·组合模式
滴_咕噜咕噜11 小时前
C#基础总结:常用的数据结构
开发语言·数据结构·c#
万兴丶14 小时前
Unity 适用于单机游戏的红点系统(前缀树 | 数据结构 | 设计模式 | 算法 | 含源码)
数据结构·unity·设计模式·c#
程序猿多布15 小时前
C#设计模式 学习笔记
设计模式·c#
软件黑马王子21 小时前
Unity游戏制作中的C#基础(5)条件语句和循环语句知识点全解析
游戏·unity·c#